source: uKadecot/trunk/kadecot/kadecot_names.c@ 101

Last change on this file since 101 was 101, checked in by coas-nagasima, 9 years ago

TOPPERS/uKadecotのソースコードを追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/plain
File size: 7.7 KB
RevLine 
[101]1/*
2 * TOPPERS ECHONET Lite Communication Middleware
3 *
4 * Copyright (C) 2015 Cores Co., Ltd. Japan
5 *
6 * ã‹L’˜ìŒ ŽÒ‚́CˆÈ‰º‚Ì(1)`(4)‚ÌðŒ‚ð–ž‚½‚·ê‡‚ÉŒÀ‚èC–{ƒ\ƒtƒgƒEƒF
7 * ƒAi–{ƒ\ƒtƒgƒEƒFƒA‚ð‰ü•Ï‚µ‚½‚à‚Ì‚ðŠÜ‚ށDˆÈ‰º“¯‚¶j‚ðŽg—pE•¡»E‰ü
8 * •ÏEÄ”z•ziˆÈ‰ºC—˜—p‚ƌĂԁj‚·‚邱‚Ƃ𖳏ž‚Å‹–‘ø‚·‚éD
9 * (1) –{ƒ\ƒtƒgƒEƒFƒA‚ðƒ\[ƒXƒR[ƒh‚ÌŒ`‚Å—˜—p‚·‚éê‡‚ɂ́Cã‹L‚Ì’˜ì
10 * Œ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’肪C‚»‚Ì‚Ü‚Ü‚ÌŒ`‚Ń\[
11 * ƒXƒR[ƒh’†‚ÉŠÜ‚Ü‚ê‚Ä‚¢‚邱‚ƁD
12 * (2) –{ƒ\ƒtƒgƒEƒFƒA‚ðCƒ‰ƒCƒuƒ‰ƒŠŒ`Ž®‚ȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
13 * —p‚Å‚«‚éŒ`‚ōĔz•z‚·‚éê‡‚ɂ́CÄ”z•z‚É”º‚¤ƒhƒLƒ…
14ƒƒ“ƒgi—˜—p
15 * ŽÒƒ}ƒjƒ…
16ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L
17 * ‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
18 * (3) –{ƒ\ƒtƒgƒEƒFƒA‚ðC‹@Ší‚É‘g‚ݍž‚ނȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
19 * —p‚Å‚«‚È‚¢Œ`‚ōĔz•z‚·‚éê‡‚ɂ́CŽŸ‚Ì‚¢‚¸‚ê‚©‚ÌðŒ‚ð–ž‚½‚·‚±
20 * ‚ƁD
21 * (a) Ä”z•z‚É”º‚¤ƒhƒLƒ…
22ƒƒ“ƒgi—˜—pŽÒƒ}ƒjƒ…
23ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜
24 * ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
25 * (b) Ä”z•z‚ÌŒ`‘Ô‚ðC•Ê‚É’è‚ß‚é•û–@‚É‚æ‚Á‚āCTOPPERSƒvƒƒWƒFƒNƒg‚É
26 * •ñ‚·‚邱‚ƁD
27 * (4) –{ƒ\ƒtƒgƒEƒFƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚é‚¢‚©‚Ȃ鑹
28 * ŠQ‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð–Ɛӂ·‚邱‚ƁD
29 * ‚Ü‚½C–{ƒ\ƒtƒgƒEƒFƒA‚̃†[ƒU‚Ü‚½‚̓Gƒ“ƒhƒ†[ƒU‚©‚ç‚Ì‚¢‚©‚Ȃ闝
30 * —R‚ÉŠî‚­¿‹‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð
31 * –Ɛӂ·‚邱‚ƁD
32 *
33 * –{ƒ\ƒtƒgƒEƒFƒA‚́C–³•ÛØ‚Å’ñ‹Ÿ‚³‚ê‚Ä‚¢‚é‚à‚Ì‚Å‚ ‚éDã‹L’˜ìŒ ŽÒ‚¨
34 * ‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚́C–{ƒ\ƒtƒgƒEƒFƒA‚ÉŠÖ‚µ‚āC“Á’è‚ÌŽg—p–Ú“I
35 * ‚ɑ΂·‚é“K‡«‚àŠÜ‚߂āC‚¢‚©‚È‚é•ÛØ‚às‚í‚È‚¢D‚Ü‚½C–{ƒ\ƒtƒgƒEƒF
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>
47typedef int bool_t;
48#define false ((bool_t)0)
49#define true ((bool_t)1)
50#endif
51#include "kadecot_names.h"
52
53uint16_t kadecot_names_get_device_type(const char *name, int nameLen);
54bool_t kadecot_names_get_device_type_name(uint16_t deviceType, char *buf, int bufLen);
55uint8_t kadecot_names_get_property_code(const char *name, int nameLen,
56 uint16_t devType);
57static uint16_t get_device_type(uint8_t *table, int len,
58 const char *name, int nameLen);
59static bool_t get_device_type_name(uint8_t *table, int len, uint16_t deviceType,
60 uint8_t *strtable, int stablelen, char *buf, int bufLen);
61static uint8_t get_property_code(uint8_t *table, int len,
62 const char *name, int nameLen, uint16_t devType);
63static 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
74uint8_t KADECOT_NAMES_TABLE[0x5000];
75#endif
76static int devTableLen;
77static uint8_t *devTable;
78static int devCodeTableLen;
79static uint8_t *devCodeTable;
80static int propTableLen;
81static uint8_t *propTable;
82
83void 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
108uint16_t kadecot_names_get_device_type(const char *name, int nameLen)
109{
110 return get_device_type(devTable, devTableLen, name, nameLen);
111}
112
113bool_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
119uint8_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
125static 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•¶Žš‚àŠÜ‚ß‚½ƒTƒCƒY */
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•¶Žš‚àŠÜ‚ß‚½ƒ`ƒFƒbƒN */
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
177static 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
234static 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•¶Žš‚àŠÜ‚ß‚½ƒTƒCƒY */
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•¶Žš‚àŠÜ‚ß‚½ƒ`ƒFƒbƒN */
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
285static 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}
Note: See TracBrowser for help on using the repository browser.