1 | /*
|
---|
2 | * TOPPERS ECHONET Lite Communication Middleware
|
---|
3 | *
|
---|
4 | * Copyright (C) 2015 Cores Co., Ltd. Japan
|
---|
5 | *
|
---|
6 | * 上記著作権者は,以下の(1)〜(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
7 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
8 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
9 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
10 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
11 | * スコード中に含まれていること.
|
---|
12 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
13 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
14 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
15 | * の無保証規定を掲載すること.
|
---|
16 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
17 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
18 | * と.
|
---|
19 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
20 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
21 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
22 | * 報告すること.
|
---|
23 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
24 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
25 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
26 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
27 | * 免責すること.
|
---|
28 | *
|
---|
29 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
30 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
31 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
32 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
33 | * の責任を負わない.
|
---|
34 | *
|
---|
35 | * @(#) $Id: kadecot_names.c 108 2015-06-11 09:15:46Z coas-nagasima $
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <string.h>
|
---|
39 | #ifndef _MSC_VER
|
---|
40 | #include <t_stddef.h>
|
---|
41 | #else
|
---|
42 | #include <stdint.h>
|
---|
43 | typedef int bool_t;
|
---|
44 | #define false ((bool_t)0)
|
---|
45 | #define true ((bool_t)1)
|
---|
46 | #endif
|
---|
47 | #include "kadecot_names.h"
|
---|
48 |
|
---|
49 | uint16_t kadecot_names_get_device_type(const char *name, int nameLen);
|
---|
50 | bool_t kadecot_names_get_device_type_name(uint16_t deviceType, char *buf, int bufLen);
|
---|
51 | uint8_t kadecot_names_get_property_code(const char *name, int nameLen,
|
---|
52 | uint16_t devType);
|
---|
53 | static uint16_t get_device_type(uint8_t *table, int len,
|
---|
54 | const char *name, int nameLen);
|
---|
55 | static bool_t get_device_type_name(uint8_t *table, int len, uint16_t deviceType,
|
---|
56 | uint8_t *strtable, int stablelen, char *buf, int bufLen);
|
---|
57 | static uint8_t get_property_code(uint8_t *table, int len,
|
---|
58 | const char *name, int nameLen, uint16_t devType);
|
---|
59 | static uint8_t get_prop_code(uint8_t *table, int len, const char *name, int nameLen,
|
---|
60 | uint16_t devType, int pos);
|
---|
61 |
|
---|
62 | #ifndef _MSC_VER
|
---|
63 | #define KADECOT_NAMES_TABLE ((uint8_t *)0xFFFE0000)
|
---|
64 | #else
|
---|
65 | #undef SIZE
|
---|
66 | #include <windows.h>
|
---|
67 | #include <tchar.h>
|
---|
68 | #include <winioctl.h>
|
---|
69 |
|
---|
70 | uint8_t KADECOT_NAMES_TABLE[0x5000];
|
---|
71 | #endif
|
---|
72 | static int devTableLen;
|
---|
73 | static uint8_t *devTable;
|
---|
74 | static int devCodeTableLen;
|
---|
75 | static uint8_t *devCodeTable;
|
---|
76 | static int propTableLen;
|
---|
77 | static uint8_t *propTable;
|
---|
78 |
|
---|
79 | void kadecot_names_init()
|
---|
80 | {
|
---|
81 | int pos;
|
---|
82 | #ifdef _MSC_VER
|
---|
83 | HANDLE h;
|
---|
84 | DWORD br;
|
---|
85 |
|
---|
86 | h = CreateFile(_T("..\\..\\..\\..\\kadecot\\kadecot_names.bin"), GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
|
---|
87 | if (h == INVALID_HANDLE_VALUE) {
|
---|
88 | return;
|
---|
89 | }
|
---|
90 |
|
---|
91 | ReadFile(h, KADECOT_NAMES_TABLE, sizeof(KADECOT_NAMES_TABLE), &br, 0);
|
---|
92 | CloseHandle(h);
|
---|
93 | #endif
|
---|
94 | devTableLen = (KADECOT_NAMES_TABLE[0] << 8) | KADECOT_NAMES_TABLE[1];
|
---|
95 | devTable = &KADECOT_NAMES_TABLE[2];
|
---|
96 | pos = devTableLen + 2;
|
---|
97 | devCodeTableLen = (KADECOT_NAMES_TABLE[pos] << 8) | KADECOT_NAMES_TABLE[pos + 1];
|
---|
98 | devCodeTable = &KADECOT_NAMES_TABLE[pos + 2];
|
---|
99 | pos += devCodeTableLen + 2;
|
---|
100 | propTableLen = (KADECOT_NAMES_TABLE[pos] << 8) | KADECOT_NAMES_TABLE[pos + 1];
|
---|
101 | propTable = &KADECOT_NAMES_TABLE[pos + 2];
|
---|
102 | }
|
---|
103 |
|
---|
104 | uint16_t kadecot_names_get_device_type(const char *name, int nameLen)
|
---|
105 | {
|
---|
106 | return get_device_type(devTable, devTableLen, name, nameLen);
|
---|
107 | }
|
---|
108 |
|
---|
109 | bool_t kadecot_names_get_device_type_name(uint16_t deviceType, char *buf, int bufLen)
|
---|
110 | {
|
---|
111 | return get_device_type_name(devCodeTable, devCodeTableLen, deviceType,
|
---|
112 | devTable, devTableLen, buf, bufLen);
|
---|
113 | }
|
---|
114 |
|
---|
115 | uint8_t kadecot_names_get_property_code(const char *name, int nameLen,
|
---|
116 | uint16_t devType)
|
---|
117 | {
|
---|
118 | return get_property_code(propTable, propTableLen, name, nameLen, devType);
|
---|
119 | }
|
---|
120 |
|
---|
121 | static uint16_t get_device_type(uint8_t *table, int len, const char *name, int nameLen)
|
---|
122 | {
|
---|
123 | int pos = 0, nextpos, i = 0;
|
---|
124 | uint8_t count;
|
---|
125 | uint16_t DeviceType;
|
---|
126 |
|
---|
127 | while (pos < len) {
|
---|
128 | /* branch count */
|
---|
129 | count = table[pos++];
|
---|
130 |
|
---|
131 | /* 末端のノード */
|
---|
132 | if (count == 0) {
|
---|
133 | /* NULL文字も含めたサイズ */
|
---|
134 | if(i != (nameLen + 1))
|
---|
135 | return 0;
|
---|
136 |
|
---|
137 | DeviceType = table[pos++] << 8;
|
---|
138 | DeviceType |= table[pos++];
|
---|
139 |
|
---|
140 | return DeviceType;
|
---|
141 | }
|
---|
142 |
|
---|
143 | for (;;) {
|
---|
144 | int length, start = pos, j, k;
|
---|
145 | /* string length */
|
---|
146 | length = table[pos++];
|
---|
147 | for (j = 0, k = i; j < length; j++, k++) {
|
---|
148 | /* NULL文字も含めたチェック */
|
---|
149 | if((k > nameLen) || (table[pos++] != name[k])){
|
---|
150 | pos = start + 1 + length + 2;
|
---|
151 | goto nextnode;
|
---|
152 | }
|
---|
153 | }
|
---|
154 | i += length;
|
---|
155 |
|
---|
156 | /* nextpos */
|
---|
157 | nextpos = table[pos++] << 8;
|
---|
158 | nextpos |= table[pos++];
|
---|
159 |
|
---|
160 | pos = nextpos;
|
---|
161 | break;
|
---|
162 | nextnode:
|
---|
163 | count--;
|
---|
164 | if (count == 0)
|
---|
165 | return 0;
|
---|
166 | continue;
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | return 0;
|
---|
171 | }
|
---|
172 |
|
---|
173 | static bool_t get_device_type_name(uint8_t *table, int len, uint16_t deviceType,
|
---|
174 | uint8_t *strtable, int stablelen, char *buf, int bufLen)
|
---|
175 | {
|
---|
176 | int pos = 0, i, unmatchpos;
|
---|
177 |
|
---|
178 | while (pos < len) {
|
---|
179 | uint16_t mask, ptrn;
|
---|
180 | /* bit mask */
|
---|
181 | mask = table[pos++] << 8;
|
---|
182 | mask |= table[pos++];
|
---|
183 | /* bit pattarn */
|
---|
184 | ptrn = table[pos++] << 8;
|
---|
185 | ptrn |= table[pos++];
|
---|
186 | /* unmatchpos */
|
---|
187 | unmatchpos = table[pos++] << 8;
|
---|
188 | unmatchpos |= table[pos++];
|
---|
189 |
|
---|
190 | if ((mask & deviceType) != ptrn) {
|
---|
191 | if (unmatchpos == 0)
|
---|
192 | return false;
|
---|
193 | pos = unmatchpos;
|
---|
194 | continue;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /* 末端のノード */
|
---|
198 | if ((mask & 0x0001) != 0) {
|
---|
199 | int bufpos = 0;
|
---|
200 | int count = table[pos++];
|
---|
201 | for (i = 0; i < count; i++) {
|
---|
202 | int substr, strlen, end;
|
---|
203 |
|
---|
204 | substr = table[pos++] << 8;
|
---|
205 | substr |= table[pos++];
|
---|
206 |
|
---|
207 | if (substr > stablelen)
|
---|
208 | return false;
|
---|
209 |
|
---|
210 | strlen = strtable[substr++];
|
---|
211 | if ((substr + strlen) > stablelen)
|
---|
212 | return false;
|
---|
213 |
|
---|
214 | end = bufpos + strlen;
|
---|
215 | if (end > bufLen)
|
---|
216 | return false;
|
---|
217 |
|
---|
218 | for (; bufpos < end; bufpos++) {
|
---|
219 | buf[bufpos] = (char)strtable[substr++];
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | return true;
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | return false;
|
---|
228 | }
|
---|
229 |
|
---|
230 | static uint8_t get_property_code(uint8_t *table, int len, const char *name,
|
---|
231 | int nameLen, uint16_t devType)
|
---|
232 | {
|
---|
233 | int pos = 0, nextpos, i = 0;
|
---|
234 | uint8_t count;
|
---|
235 | uint8_t epc;
|
---|
236 |
|
---|
237 | while (pos < len) {
|
---|
238 | /* branch count */
|
---|
239 | count = table[pos++];
|
---|
240 |
|
---|
241 | /* 末端のノード */
|
---|
242 | if (count == 0) {
|
---|
243 | /* NULL文字も含めたサイズ */
|
---|
244 | if(i != (nameLen + 1))
|
---|
245 | return 0;
|
---|
246 |
|
---|
247 | epc = get_prop_code(table, len, name, nameLen, devType, pos);
|
---|
248 | return epc;
|
---|
249 | }
|
---|
250 |
|
---|
251 | for (;;) {
|
---|
252 | int length, start = pos, j, k;
|
---|
253 | /* string length */
|
---|
254 | length = table[pos++];
|
---|
255 | for (j = 0, k = i; j < length; j++, k++) {
|
---|
256 | /* NULL文字も含めたチェック */
|
---|
257 | if((k > nameLen) || (table[pos++] != name[k])){
|
---|
258 | pos = start + 1 + length + 2;
|
---|
259 | goto nextnode;
|
---|
260 | }
|
---|
261 | }
|
---|
262 | i += length;
|
---|
263 |
|
---|
264 | /* nextpos */
|
---|
265 | nextpos = table[pos++] << 8;
|
---|
266 | nextpos |= table[pos++];
|
---|
267 |
|
---|
268 | pos = nextpos;
|
---|
269 | break;
|
---|
270 | nextnode:
|
---|
271 | count--;
|
---|
272 | if (count == 0)
|
---|
273 | return 0;
|
---|
274 | continue;
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | return 0;
|
---|
279 | }
|
---|
280 |
|
---|
281 | static uint8_t get_prop_code(uint8_t *table, int len, const char *name,
|
---|
282 | int nameLen, uint16_t devType, int pos)
|
---|
283 | {
|
---|
284 | int i, start;
|
---|
285 | uint8_t count;
|
---|
286 | uint8_t epc;
|
---|
287 | uint16_t type;
|
---|
288 |
|
---|
289 | while (pos < len) {
|
---|
290 | /* branch count */
|
---|
291 | count = table[pos++];
|
---|
292 |
|
---|
293 | /* 末端のノード */
|
---|
294 | if (count == 0) {
|
---|
295 | epc = table[pos++];
|
---|
296 | return epc;
|
---|
297 | }
|
---|
298 |
|
---|
299 | start = pos;
|
---|
300 | for (i = 0; i < count; i++) {
|
---|
301 | type = table[pos++] << 8;
|
---|
302 | type |= table[pos++];
|
---|
303 | if (type == devType) {
|
---|
304 | epc = table[start + 2 * count];
|
---|
305 | return epc;
|
---|
306 | }
|
---|
307 | }
|
---|
308 | pos++;
|
---|
309 | }
|
---|
310 |
|
---|
311 | epc = table[pos];
|
---|
312 | return epc;
|
---|
313 | }
|
---|