Main Page | Data Structures | File List | Data Fields | Globals | Related Pages

presentation.c

00001 /***
00002  * This file is part of OpenHome, an open source home automation system.
00003  * Copyright (C) 2003 Jan Klötzke
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #include <avr/pgmspace.h>
00021 #include <inttypes.h>
00022 #include "net/presentation.h"
00023 
00024 static prog_uchar length_table[] = {0, 0, 1, 2, 4, 8, 4};
00025 static uint8_t *tag_position, *dat_position;
00026 static uint8_t length;
00027 
00028 /***
00029  * Prepare to parse a given buffer.
00030  */
00031 void prs_init(uint8_t* buf, uint8_t len)
00032 {
00033         tag_position = buf;
00034         dat_position = (uint8_t*)0;
00035         length = len;
00036 }
00037 
00038 /***
00039  * Return current tag and advance to next one.
00040  *
00041  * If the end of the current buffer is reached 0xff will be returned.
00042  */
00043 uint8_t prs_get_tag(void)
00044 {
00045         uint8_t result, size;
00046         
00047         if (length == 0) return 0xff;
00048         
00049         result = *tag_position;
00050         tag_position++;
00051         dat_position = tag_position;
00052         length--;
00053         if ((result & P_TYPE_MASK) == P_TYPE_STRING) {
00054                 // skip strings
00055                 while ((*tag_position != 0) && (length-- > 0)) tag_position++;
00056                 if (length > 0) {
00057                         tag_position++;
00058                         length--;
00059                 }
00060         } else {
00061                 size = __LPM(length_table+(result & P_TYPE_MASK));
00062                 if (size <= length) {
00063                         tag_position += size;
00064                         length -= size;
00065                 } else length = 0;
00066         }
00067         return result;
00068 }
00069 
00070 /***
00071  * Skip current tag.
00072  *
00073  * Parameter "tag" must hold the tag which was peviously returned
00074  * by "prs_get_tag()".
00075  */
00076 void prs_skip(uint8_t tag)
00077 {
00078         uint8_t level;
00079         
00080         // was the last tag a compound tag or only a value?
00081         if ((tag & P_TYPE_MASK) != P_TYPE_COMPOUND_OPEN) return;
00082         
00083         // we are inside a compund tag, skip it with all contents
00084         level = 1;
00085         do {
00086                 tag = prs_get_tag();
00087                 if (tag == 0xff) return;
00088                 switch (tag & P_TYPE_MASK) {
00089                         case P_TYPE_COMPOUND_OPEN:
00090                                 level++;
00091                                 break;
00092                         case P_TYPE_COMPOUND_CLOSE:
00093                                 level--;
00094                                 break;
00095                         default:
00096                                 break;
00097                 }
00098         } while (level > 0);
00099 }
00100 
00101 uint8_t prs_get_char(void)
00102 {
00103         return *dat_position;
00104 }
00105 
00106 uint16_t prs_get_int(void)
00107 {
00108         return *((uint16_t*)dat_position);
00109 }
00110 
00111 char* prs_get_str(void)
00112 {
00113         return dat_position;
00114 }

Generated on Fri Oct 17 16:45:54 2003 for OpenHome by doxygen 1.3.3