[101] | 1 | /*
|
---|
| 2 | * TOPPERS ECHONET Lite Communication Middleware
|
---|
| 3 | *
|
---|
| 4 | * Copyright (C) 2015 Cores Co., Ltd. Japan
|
---|
| 5 | *
|
---|
| 6 | * ãLì ÒÍCȺÌ(1)`(4)Ìðð½·êÉÀèC{\tgEF
|
---|
| 7 | * Ai{\tgEFAðüϵ½àÌðÜÞDȺ¯¶jðgpE¡»Eü
|
---|
| 8 | * ÏEÄzziȺCpÆÄÔj·é±Æð³Åø·éD
|
---|
| 9 | * (1) {\tgEFAð\[XR[hÌ`Åp·éêÉÍCãLÌì
|
---|
| 10 | * \¦C±Ìpð¨æѺL̳ÛØKèªC»ÌÜÜÌ`Å\[
|
---|
| 11 | * XR[hÉÜÜêÄ¢é±ÆD
|
---|
| 12 | * (2) {\tgEFAðCCu`®ÈÇC¼Ì\tgEFAJÉg
|
---|
| 13 | * pÅ«é`ÅÄzz·éêÉÍCÄzzɺ¤hL
|
---|
| 14 | gip
|
---|
| 15 | * Ò}j
|
---|
| 16 | AÈÇjÉCãLÌì \¦C±Ìpð¨æѺL
|
---|
| 17 | * ̳ÛØKèðfÚ·é±ÆD
|
---|
| 18 | * (3) {\tgEFAðC@íÉgÝÞÈÇC¼Ì\tgEFAJÉg
|
---|
| 19 | * pÅ«È¢`ÅÄzz·éêÉÍCÌ¢¸ê©Ìðð½·±
|
---|
| 20 | * ÆD
|
---|
| 21 | * (a) Äzzɺ¤hL
|
---|
| 22 | gipÒ}j
|
---|
| 23 | AÈÇjÉCãLÌ
|
---|
| 24 | * ì \¦C±Ìpð¨æѺL̳ÛØKèðfÚ·é±ÆD
|
---|
| 25 | * (b) ÄzzÌ`ÔðCÊÉèßéû@ÉæÁÄCTOPPERSvWFNgÉ
|
---|
| 26 | * ñ·é±ÆD
|
---|
| 27 | * (4) {\tgEFAÌpÉæè¼ÚIܽÍÔÚIɶ¶é¢©Èé¹
|
---|
| 28 | * Q©çàCãLì Ò¨æÑTOPPERSvWFNgðÆÓ·é±ÆD
|
---|
| 29 | * ܽC{\tgEFAÌ[UܽÍGh[U©çÌ¢©Èé
|
---|
| 30 | * RÉîÿ©çàCãLì Ò¨æÑTOPPERSvWFNgð
|
---|
| 31 | * ÆÓ·é±ÆD
|
---|
| 32 | *
|
---|
| 33 | * {\tgEFAÍC³ÛØÅñ³êÄ¢éàÌÅ éDãLì Ò¨
|
---|
| 34 | * æÑTOPPERSvWFNgÍC{\tgEFAÉÖµÄCÁèÌgpÚI
|
---|
| 35 | * ÉηéK«àÜßÄC¢©ÈéÛØàsíÈ¢DܽC{\tgEF
|
---|
| 36 | * AÌpÉæè¼ÚIܽÍÔÚIɶ¶½¢©Èé¹QÉÖµÄàC»
|
---|
| 37 | * ÌÓCðíÈ¢D
|
---|
| 38 | *
|
---|
| 39 | * @(#) $Id: kadecot_names.c 101 2015-06-02 15:37:23Z coas-nagasima $
|
---|
| 40 | */
|
---|
| 41 |
|
---|
| 42 | #include <string.h>
|
---|
| 43 | #ifndef _MSC_VER
|
---|
| 44 | #include <t_stddef.h>
|
---|
| 45 | #else
|
---|
| 46 | #include <stdint.h>
|
---|
| 47 | typedef int bool_t;
|
---|
| 48 | #define false ((bool_t)0)
|
---|
| 49 | #define true ((bool_t)1)
|
---|
| 50 | #endif
|
---|
| 51 | #include "kadecot_names.h"
|
---|
| 52 |
|
---|
| 53 | uint16_t kadecot_names_get_device_type(const char *name, int nameLen);
|
---|
| 54 | bool_t kadecot_names_get_device_type_name(uint16_t deviceType, char *buf, int bufLen);
|
---|
| 55 | uint8_t kadecot_names_get_property_code(const char *name, int nameLen,
|
---|
| 56 | uint16_t devType);
|
---|
| 57 | static uint16_t get_device_type(uint8_t *table, int len,
|
---|
| 58 | const char *name, int nameLen);
|
---|
| 59 | static bool_t get_device_type_name(uint8_t *table, int len, uint16_t deviceType,
|
---|
| 60 | uint8_t *strtable, int stablelen, char *buf, int bufLen);
|
---|
| 61 | static uint8_t get_property_code(uint8_t *table, int len,
|
---|
| 62 | const char *name, int nameLen, uint16_t devType);
|
---|
| 63 | static uint8_t get_prop_code(uint8_t *table, int len, const char *name, int nameLen,
|
---|
| 64 | uint16_t devType, int pos);
|
---|
| 65 |
|
---|
| 66 | #ifndef _MSC_VER
|
---|
| 67 | #define KADECOT_NAMES_TABLE ((uint8_t *)0xFFFE0000)
|
---|
| 68 | #else
|
---|
| 69 | #undef SIZE
|
---|
| 70 | #include <windows.h>
|
---|
| 71 | #include <tchar.h>
|
---|
| 72 | #include <winioctl.h>
|
---|
| 73 |
|
---|
| 74 | uint8_t KADECOT_NAMES_TABLE[0x5000];
|
---|
| 75 | #endif
|
---|
| 76 | static int devTableLen;
|
---|
| 77 | static uint8_t *devTable;
|
---|
| 78 | static int devCodeTableLen;
|
---|
| 79 | static uint8_t *devCodeTable;
|
---|
| 80 | static int propTableLen;
|
---|
| 81 | static uint8_t *propTable;
|
---|
| 82 |
|
---|
| 83 | void kadecot_names_init()
|
---|
| 84 | {
|
---|
| 85 | int pos;
|
---|
| 86 | #ifdef _MSC_VER
|
---|
| 87 | HANDLE h;
|
---|
| 88 | DWORD br;
|
---|
| 89 |
|
---|
| 90 | h = CreateFile(_T("..\\..\\..\\..\\kadecot\\kadecot_names.bin"), GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
|
---|
| 91 | if (h == INVALID_HANDLE_VALUE) {
|
---|
| 92 | return;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | ReadFile(h, KADECOT_NAMES_TABLE, sizeof(KADECOT_NAMES_TABLE), &br, 0);
|
---|
| 96 | CloseHandle(h);
|
---|
| 97 | #endif
|
---|
| 98 | devTableLen = (KADECOT_NAMES_TABLE[0] << 8) | KADECOT_NAMES_TABLE[1];
|
---|
| 99 | devTable = &KADECOT_NAMES_TABLE[2];
|
---|
| 100 | pos = devTableLen + 2;
|
---|
| 101 | devCodeTableLen = (KADECOT_NAMES_TABLE[pos] << 8) | KADECOT_NAMES_TABLE[pos + 1];
|
---|
| 102 | devCodeTable = &KADECOT_NAMES_TABLE[pos + 2];
|
---|
| 103 | pos += devCodeTableLen + 2;
|
---|
| 104 | propTableLen = (KADECOT_NAMES_TABLE[pos] << 8) | KADECOT_NAMES_TABLE[pos + 1];
|
---|
| 105 | propTable = &KADECOT_NAMES_TABLE[pos + 2];
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | uint16_t kadecot_names_get_device_type(const char *name, int nameLen)
|
---|
| 109 | {
|
---|
| 110 | return get_device_type(devTable, devTableLen, name, nameLen);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | bool_t kadecot_names_get_device_type_name(uint16_t deviceType, char *buf, int bufLen)
|
---|
| 114 | {
|
---|
| 115 | return get_device_type_name(devCodeTable, devCodeTableLen, deviceType,
|
---|
| 116 | devTable, devTableLen, buf, bufLen);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | uint8_t kadecot_names_get_property_code(const char *name, int nameLen,
|
---|
| 120 | uint16_t devType)
|
---|
| 121 | {
|
---|
| 122 | return get_property_code(propTable, propTableLen, name, nameLen, devType);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | static uint16_t get_device_type(uint8_t *table, int len, const char *name, int nameLen)
|
---|
| 126 | {
|
---|
| 127 | int pos = 0, nextpos, i = 0;
|
---|
| 128 | uint8_t count;
|
---|
| 129 | uint16_t DeviceType;
|
---|
| 130 |
|
---|
| 131 | while (pos < len) {
|
---|
| 132 | /* branch count */
|
---|
| 133 | count = table[pos++];
|
---|
| 134 |
|
---|
| 135 | /* [Ìm[h */
|
---|
| 136 | if (count == 0) {
|
---|
| 137 | /* NULL¶àÜß½TCY */
|
---|
| 138 | if(i != (nameLen + 1))
|
---|
| 139 | return 0;
|
---|
| 140 |
|
---|
| 141 | DeviceType = table[pos++] << 8;
|
---|
| 142 | DeviceType |= table[pos++];
|
---|
| 143 |
|
---|
| 144 | return DeviceType;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | for (;;) {
|
---|
| 148 | int length, start = pos, j, k;
|
---|
| 149 | /* string length */
|
---|
| 150 | length = table[pos++];
|
---|
| 151 | for (j = 0, k = i; j < length; j++, k++) {
|
---|
| 152 | /* NULL¶àÜß½`FbN */
|
---|
| 153 | if((k > nameLen) || (table[pos++] != name[k])){
|
---|
| 154 | pos = start + 1 + length + 2;
|
---|
| 155 | goto nextnode;
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 | i += length;
|
---|
| 159 |
|
---|
| 160 | /* nextpos */
|
---|
| 161 | nextpos = table[pos++] << 8;
|
---|
| 162 | nextpos |= table[pos++];
|
---|
| 163 |
|
---|
| 164 | pos = nextpos;
|
---|
| 165 | break;
|
---|
| 166 | nextnode:
|
---|
| 167 | count--;
|
---|
| 168 | if (count == 0)
|
---|
| 169 | return 0;
|
---|
| 170 | continue;
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | return 0;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | static bool_t get_device_type_name(uint8_t *table, int len, uint16_t deviceType,
|
---|
| 178 | uint8_t *strtable, int stablelen, char *buf, int bufLen)
|
---|
| 179 | {
|
---|
| 180 | int pos = 0, i, unmatchpos;
|
---|
| 181 |
|
---|
| 182 | while (pos < len) {
|
---|
| 183 | uint16_t mask, ptrn;
|
---|
| 184 | /* bit mask */
|
---|
| 185 | mask = table[pos++] << 8;
|
---|
| 186 | mask |= table[pos++];
|
---|
| 187 | /* bit pattarn */
|
---|
| 188 | ptrn = table[pos++] << 8;
|
---|
| 189 | ptrn |= table[pos++];
|
---|
| 190 | /* unmatchpos */
|
---|
| 191 | unmatchpos = table[pos++] << 8;
|
---|
| 192 | unmatchpos |= table[pos++];
|
---|
| 193 |
|
---|
| 194 | if ((mask & deviceType) != ptrn) {
|
---|
| 195 | if (unmatchpos == 0)
|
---|
| 196 | return false;
|
---|
| 197 | pos = unmatchpos;
|
---|
| 198 | continue;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | /* [Ìm[h */
|
---|
| 202 | if ((mask & 0x0001) != 0) {
|
---|
| 203 | int bufpos = 0;
|
---|
| 204 | int count = table[pos++];
|
---|
| 205 | for (i = 0; i < count; i++) {
|
---|
| 206 | int substr, strlen, end;
|
---|
| 207 |
|
---|
| 208 | substr = table[pos++] << 8;
|
---|
| 209 | substr |= table[pos++];
|
---|
| 210 |
|
---|
| 211 | if (substr > stablelen)
|
---|
| 212 | return false;
|
---|
| 213 |
|
---|
| 214 | strlen = strtable[substr++];
|
---|
| 215 | if ((substr + strlen) > stablelen)
|
---|
| 216 | return false;
|
---|
| 217 |
|
---|
| 218 | end = bufpos + strlen;
|
---|
| 219 | if (end > bufLen)
|
---|
| 220 | return false;
|
---|
| 221 |
|
---|
| 222 | for (; bufpos < end; bufpos++) {
|
---|
| 223 | buf[bufpos] = (char)strtable[substr++];
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | return true;
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | return false;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | static uint8_t get_property_code(uint8_t *table, int len, const char *name,
|
---|
| 235 | int nameLen, uint16_t devType)
|
---|
| 236 | {
|
---|
| 237 | int pos = 0, nextpos, i = 0;
|
---|
| 238 | uint8_t count;
|
---|
| 239 | uint8_t epc;
|
---|
| 240 |
|
---|
| 241 | while (pos < len) {
|
---|
| 242 | /* branch count */
|
---|
| 243 | count = table[pos++];
|
---|
| 244 |
|
---|
| 245 | /* [Ìm[h */
|
---|
| 246 | if (count == 0) {
|
---|
| 247 | /* NULL¶àÜß½TCY */
|
---|
| 248 | if(i != (nameLen + 1))
|
---|
| 249 | return 0;
|
---|
| 250 |
|
---|
| 251 | epc = get_prop_code(table, len, name, nameLen, devType, pos);
|
---|
| 252 | return epc;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | for (;;) {
|
---|
| 256 | int length, start = pos, j, k;
|
---|
| 257 | /* string length */
|
---|
| 258 | length = table[pos++];
|
---|
| 259 | for (j = 0, k = i; j < length; j++, k++) {
|
---|
| 260 | /* NULL¶àÜß½`FbN */
|
---|
| 261 | if((k > nameLen) || (table[pos++] != name[k])){
|
---|
| 262 | pos = start + 1 + length + 2;
|
---|
| 263 | goto nextnode;
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
| 266 | i += length;
|
---|
| 267 |
|
---|
| 268 | /* nextpos */
|
---|
| 269 | nextpos = table[pos++] << 8;
|
---|
| 270 | nextpos |= table[pos++];
|
---|
| 271 |
|
---|
| 272 | pos = nextpos;
|
---|
| 273 | break;
|
---|
| 274 | nextnode:
|
---|
| 275 | count--;
|
---|
| 276 | if (count == 0)
|
---|
| 277 | return 0;
|
---|
| 278 | continue;
|
---|
| 279 | }
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | return 0;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | static uint8_t get_prop_code(uint8_t *table, int len, const char *name,
|
---|
| 286 | int nameLen, uint16_t devType, int pos)
|
---|
| 287 | {
|
---|
| 288 | int i, start;
|
---|
| 289 | uint8_t count;
|
---|
| 290 | uint8_t epc;
|
---|
| 291 | uint16_t type;
|
---|
| 292 |
|
---|
| 293 | while (pos < len) {
|
---|
| 294 | /* branch count */
|
---|
| 295 | count = table[pos++];
|
---|
| 296 |
|
---|
| 297 | /* [Ìm[h */
|
---|
| 298 | if (count == 0) {
|
---|
| 299 | epc = table[pos++];
|
---|
| 300 | return epc;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | start = pos;
|
---|
| 304 | for (i = 0; i < count; i++) {
|
---|
| 305 | type = table[pos++] << 8;
|
---|
| 306 | type |= table[pos++];
|
---|
| 307 | if (type == devType) {
|
---|
| 308 | epc = table[start + 2 * count];
|
---|
| 309 | return epc;
|
---|
| 310 | }
|
---|
| 311 | }
|
---|
| 312 | pos++;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | epc = table[pos];
|
---|
| 316 | return epc;
|
---|
| 317 | }
|
---|