1 /*
2 * Copyright (C) 2015-2019 Alibaba Group Holding Limited
3 */
4
5 #define CONFIG_LOGMACRO_DETAILS
6
7 #include "amp_platform.h"
8 #include "amp_config.h"
9 #include "aos_system.h"
10 #include "aos_fs.h"
11 #include "amp_defines.h"
12 #include "board_mgr.h"
13 #include "board_marker.h"
14 //#include "core_list.h"
15
16 #include "cJSON.h"
17
18 #ifdef JSE_ADVANCED_ADDON_UI
19 #include "render.h"
20 void page_config_parse();
21 volatile int app_run_flag = 0;
22 #endif
23
24 #define MOD_STR "BOARD_MGR"
25 #define DRIVER_DIR JSE_FS_ROOT_DIR "/drivers/"
26
27 #define DRIVER_NAME "driver.json"
28
29 extern int g_repl_config;
30
31 typedef struct parse_json
32 {
33 char *marker_name;
34 addon_module_m module;
35 int8_t (*fn)(cJSON *, char *);
36 } parse_json_t;
37
38 typedef struct board_item
39 {
40 addon_module_m module;
41 item_handle_t handle;
42 char *name_id;
43 void *node;
44 uint8_t status;
45 } board_item_t;
46
47 typedef struct board_mgr
48 {
49 uint32_t item_size;
50 board_item_t **item;
51 } board_mgr_t;
52
53 #if 0
54 typedef struct page_entry
55 {
56 char *page;
57 struct core_list_head node;
58 }page_entry_t;
59 #endif
60
61 extern void aos_userjs_version_set(char *version);
62
63 static board_mgr_t g_board_mgr = {0, NULL};
64
65 //static struct core_list_head g_pages_list;
66
67 static int8_t board_add_new_item(addon_module_m module, char *name_id,
68 void *node);
69
board_get_handle(void)70 static board_mgr_t *board_get_handle(void)
71 {
72 return &g_board_mgr;
73 }
74
75 #ifdef JSE_HW_ADDON_GPIO
76 #include "aos_hal_gpio.h"
board_set_gpio_default(gpio_dev_t * gpio_device)77 static void board_set_gpio_default(gpio_dev_t *gpio_device)
78 {
79 gpio_params_t *priv = (gpio_params_t *)gpio_device->priv;
80 if (NULL == gpio_device || NULL == priv)
81 {
82 return;
83 }
84
85 gpio_device->port = -1;
86 gpio_device->config = OUTPUT_PUSH_PULL;
87 gpio_device->gpioc = NULL;
88 gpio_device->gpioc_index = 0;
89 gpio_device->pin_index = 0;
90 priv->irq_mode = 0;
91 priv->js_cb_ref = 0;
92 priv->reserved = NULL;
93 }
94
board_parse_gpio(cJSON * gpio,char * id)95 static int8_t board_parse_gpio(cJSON *gpio, char *id)
96 {
97 int index = 0;
98 int8_t ret = -1;
99 cJSON *port = NULL;
100 cJSON *item = NULL;
101 cJSON *dir = NULL;
102 cJSON *pull = NULL;
103 cJSON *intMode = NULL;
104 gpio_dev_t device;
105 gpio_params_t *priv = NULL;
106 gpio_config_t *config = (gpio_config_t *)&device.config;
107 int8_t size = 1;
108
109 if (size <= 0)
110 {
111 return (-1);
112 }
113 while (index < size)
114 {
115 priv = (gpio_params_t *)amp_calloc(1, sizeof(gpio_params_t));
116 if (priv == NULL) {
117 amp_error(MOD_STR, "malloc failed");
118 return (-1);
119 }
120 item = gpio;
121 index += 1;
122 if (NULL == item || NULL == id)
123 {
124 continue;
125 }
126 port = cJSON_GetObjectItem(item, MARKER_PORT);
127 if (NULL == port || cJSON_Number != port->type)
128 {
129 continue;
130 }
131
132 device.priv = priv;
133 board_set_gpio_default(&device);
134 dir = cJSON_GetObjectItem(item, GPIO_DIR);
135 pull = cJSON_GetObjectItem(item, GPIO_PULL);
136 intMode = cJSON_GetObjectItem(item, GPIO_INTMODE);
137 if (NULL != dir && cJSON_String == dir->type && NULL != pull &&
138 cJSON_String == pull->type)
139 {
140 if (strcmp(dir->valuestring, GPIO_DIR_INPUT) == 0) {
141 if (strcmp(GPIO_PULL_DOWN, pull->valuestring) == 0)
142 *config = INPUT_PULL_DOWN;
143 else if (strcmp(GPIO_PULL_UP, pull->valuestring) == 0)
144 *config = INPUT_PULL_UP;
145 else if (strcmp(GPIO_PULL_OPEN, pull->valuestring) == 0)
146 *config = INPUT_HIGH_IMPEDANCE;
147 }
148
149 else if (strcmp(dir->valuestring, GPIO_DIR_OUTPUT) == 0) {
150 if (strcmp(GPIO_PULL_DOWN, pull->valuestring) == 0)
151 *config = OUTPUT_PUSH_PULL;
152 else if (strcmp(GPIO_PULL_UP, pull->valuestring) == 0)
153 *config = OUTPUT_OPEN_DRAIN_PULL_UP;
154 else if (strcmp(GPIO_PULL_OPEN, pull->valuestring) == 0)
155 *config = OUTPUT_OPEN_DRAIN_NO_PULL;
156 }
157
158 else if (strcmp(dir->valuestring, GPIO_DIR_IRQ) == 0) {
159 *config = IRQ_MODE;
160
161 if (strcmp(GPIO_PULL_DOWN, pull->valuestring) == 0)
162 *config = INPUT_PULL_DOWN;
163 else if (strcmp(GPIO_PULL_UP, pull->valuestring) == 0)
164 *config = INPUT_PULL_UP;
165 else if (strcmp(GPIO_PULL_OPEN, pull->valuestring) == 0)
166 *config = INPUT_HIGH_IMPEDANCE;
167
168 if (strcmp(GPIO_INT_RISING, intMode->valuestring) == 0)
169 priv->irq_mode = IRQ_TRIGGER_RISING_EDGE;
170 else if (strcmp(GPIO_INT_FALLING, intMode->valuestring) == 0)
171 priv->irq_mode = IRQ_TRIGGER_FALLING_EDGE;
172 else if (strcmp(GPIO_INT_BOTH, intMode->valuestring) == 0)
173 priv->irq_mode = IRQ_TRIGGER_BOTH_EDGES;
174 else if (strcmp(GPIO_INT_HIGH_LEVEL, intMode->valuestring) == 0)
175 priv->irq_mode = IRQ_TRIGGER_LEVEL_HIGH;
176 else if (strcmp(GPIO_INT_LOW_LEVEL, intMode->valuestring) == 0)
177 priv->irq_mode = IRQ_TRIGGER_LEVEL_LOW;
178 }
179
180 else if (strcmp(dir->valuestring, GPIO_DIR_ANALOG) == 0) {
181 *config = ANALOG_MODE;
182 }
183 }
184 gpio_dev_t *new_gpio = amp_calloc(1, sizeof(*new_gpio));
185 if (NULL == new_gpio)
186 {
187 continue;
188 }
189 device.port = port->valueint;
190 char *gpio_id = strdup(id);
191 memcpy(new_gpio, &device, sizeof(gpio_dev_t));
192 ret = board_add_new_item(MODULE_GPIO, gpio_id, new_gpio);
193 if (0 == ret)
194 {
195 continue;
196 }
197 if (NULL != gpio_id)
198 {
199 amp_free(gpio_id);
200 gpio_id = NULL;
201 }
202 if (NULL != new_gpio)
203 {
204 amp_free(new_gpio);
205 new_gpio = NULL;
206 amp_free(priv);
207 priv = NULL;
208 }
209 }
210 return (0);
211 }
212 #endif
213
214 #ifdef JSE_HW_ADDON_UART
215 #include "aos_hal_uart.h"
board_set_uart_default(uart_dev_t * uart_device)216 static void board_set_uart_default(uart_dev_t *uart_device)
217 {
218 if (NULL == uart_device)
219 {
220 return;
221 }
222 uart_device->port = 0;
223 uart_device->priv = NULL;
224 uart_device->config.baud_rate = 115200;
225 uart_device->config.data_width = DATA_WIDTH_8BIT;
226 uart_device->config.flow_control = FLOW_CONTROL_DISABLED;
227 uart_device->config.parity = NO_PARITY;
228 uart_device->config.stop_bits = STOP_BITS_1;
229 uart_device->config.mode = MODE_TX_RX;
230 }
231
board_parse_uart(cJSON * uart,char * id)232 static int8_t board_parse_uart(cJSON *uart, char *id)
233 {
234 int index = 0;
235 int8_t ret = -1;
236 cJSON *port = NULL;
237 cJSON *item = NULL;
238 cJSON *temp = NULL;
239 uart_dev_t device;
240 uart_config_t *config = (uart_config_t *)&device.config;
241 int8_t size = 1;
242
243 if (size <= 0)
244 {
245 return (-1);
246 }
247 while (index < size)
248 {
249 item = uart;
250 index += 1;
251 if (NULL == item)
252 {
253 continue;
254 }
255 port = cJSON_GetObjectItem(item, MARKER_PORT);
256 if (NULL == port || cJSON_Number != port->type)
257 {
258 continue;
259 }
260 board_set_uart_default(&device);
261 temp = cJSON_GetObjectItem(item, UART_DATA_WIDTH);
262 if (NULL != temp && cJSON_Number == temp->type)
263 {
264 int32_t width = temp->valueint;
265 switch (width)
266 {
267 case 5:
268 config->data_width = DATA_WIDTH_5BIT;
269 break;
270 case 6:
271 config->data_width = DATA_WIDTH_6BIT;
272 break;
273 case 7:
274 config->data_width = DATA_WIDTH_7BIT;
275 break;
276 case 8:
277 config->data_width = DATA_WIDTH_8BIT;
278 break;
279
280 default:
281 break;
282 }
283 }
284 temp = cJSON_GetObjectItem(item, UART_BAUD_RATE);
285 if (NULL != temp && cJSON_Number == temp->type)
286 {
287 config->baud_rate = temp->valueint;
288 }
289 temp = cJSON_GetObjectItem(item, UART_STOP_BITS);
290 if (NULL != temp && cJSON_Number == temp->type)
291 {
292 int32_t stopbits = temp->valueint;
293 switch (stopbits)
294 {
295 case 1:
296 config->stop_bits = STOP_BITS_1;
297 break;
298 case 2:
299 config->stop_bits = STOP_BITS_2;
300
301 default:
302 break;
303 }
304 }
305 temp = cJSON_GetObjectItem(item, UART_FLOW_CONTROL);
306 if (NULL != temp && cJSON_String == temp->type)
307 {
308 if (strcmp(temp->valuestring, UART_FC_DISABLE) == 0) {
309 config->flow_control = FLOW_CONTROL_DISABLED;
310 }
311
312 else if (strcmp(temp->valuestring, UART_FC_CTS) == 0) {
313 config->flow_control = FLOW_CONTROL_CTS;
314 }
315
316 else if (strcmp(temp->valuestring, UART_FC_RTS) == 0) {
317 config->flow_control = FLOW_CONTROL_RTS;
318 }
319
320 else if (strcmp(temp->valuestring, UART_FC_RTSCTS) == 0) {
321 config->flow_control = FLOW_CONTROL_CTS_RTS;
322 }
323 }
324 temp = cJSON_GetObjectItem(item, UART_PARITY_CONFIG);
325 if (NULL != temp && cJSON_String == temp->type)
326 {
327 if (strcmp(temp->valuestring, UART_PARITY_NONE) == 0) {
328 config->parity = NO_PARITY;
329 }
330
331 else if (strcmp(temp->valuestring, UART_PARITY_ODD) == 0) {
332 config->parity = ODD_PARITY;
333 }
334
335 else if (strcmp(temp->valuestring, UART_PARITY_EVEN) == 0) {
336 config->parity = EVEN_PARITY;
337 }
338 }
339 uart_dev_t *new_uart = amp_calloc(1, sizeof(*new_uart));
340 if (NULL == new_uart)
341 {
342 continue;
343 }
344 device.port = port->valueint;
345 char *uart_id = strdup(id);
346 *new_uart = device;
347 ret = board_add_new_item(MODULE_UART, uart_id, new_uart);
348 amp_debug(MOD_STR, "*** add item: %s", uart_id);
349 if (0 == ret)
350 {
351 continue;
352 }
353 if (NULL != uart_id)
354 {
355 amp_free(uart_id);
356 uart_id = NULL;
357 }
358 if (NULL != new_uart)
359 {
360 amp_free(new_uart);
361 new_uart = NULL;
362 }
363 }
364
365 return (0);
366 }
367 #endif
368
369 #ifdef JSE_HW_ADDON_I2C
370 #include "aos_hal_i2c.h"
board_set_i2c_default(i2c_dev_t * i2c_device)371 static void board_set_i2c_default(i2c_dev_t *i2c_device)
372 {
373 if (NULL == i2c_device)
374 {
375 return;
376 }
377 i2c_device->port = 0;
378 i2c_device->priv = NULL;
379 i2c_device->config.address_width = 7;
380 i2c_device->config.freq = 100000;
381 i2c_device->config.mode = I2C_MODE_MASTER;
382 i2c_device->config.dev_addr = 0xFF;
383 }
384
board_parse_i2c(cJSON * i2c,char * id)385 static int8_t board_parse_i2c(cJSON *i2c, char *id)
386 {
387 int index = 0;
388 int8_t ret = -1;
389 cJSON *port = NULL;
390 cJSON *item = NULL;
391 cJSON *temp = NULL;
392 i2c_dev_t device;
393 i2c_config_t *config = (i2c_config_t *)&device.config;
394 int8_t size = 1;
395
396 if (size <= 0)
397 {
398 return (-1);
399 }
400 while (index < size)
401 {
402 item = i2c;
403 index += 1;
404 if (NULL == item)
405 {
406 continue;
407 }
408 port = cJSON_GetObjectItem(item, MARKER_PORT);
409 if (NULL == port || cJSON_Number != port->type)
410 {
411 continue;
412 }
413 board_set_i2c_default(&device);
414
415 temp = cJSON_GetObjectItem(item, I2C_ADDR_WIDTH);
416 if (NULL != temp && cJSON_Number == temp->type)
417 {
418 switch (temp->valueint)
419 {
420 case 7:
421 config->address_width = I2C_HAL_ADDRESS_WIDTH_7BIT;
422 break;
423 case 10:
424 config->address_width = I2C_HAL_ADDRESS_WIDTH_10BIT;
425 break;
426
427 default:
428 break;
429 }
430 }
431 temp = cJSON_GetObjectItem(item, I2C_FREQ);
432 if (NULL != temp && cJSON_Number == temp->type)
433 {
434 config->freq = temp->valueint;
435 }
436 temp = cJSON_GetObjectItem(item, I2C_MODE);
437 if (NULL != temp && cJSON_String == temp->type)
438 {
439 if (strcmp(temp->valuestring, I2C_MASTER) == 0) {
440 config->mode = I2C_MODE_MASTER;
441 }
442
443 else if (strcmp(temp->valuestring, I2C_SLAVE) == 0) {
444 config->mode = I2C_MODE_SLAVE;
445 }
446 }
447 temp = cJSON_GetObjectItem(item, I2C_ADDR_DEV);
448 if (NULL != temp && cJSON_Number == temp->type)
449 {
450 config->dev_addr = temp->valueint;
451 }
452
453 i2c_dev_t *new_i2c = amp_calloc(1, sizeof(*new_i2c));
454 if (NULL == new_i2c)
455 {
456 continue;
457 }
458 device.port = port->valueint;
459 char *i2c_id = strdup(id);
460 *new_i2c = device;
461 ret = board_add_new_item(MODULE_I2C, i2c_id, new_i2c);
462 if (0 == ret)
463 {
464 continue;
465 }
466 if (NULL != i2c_id)
467 {
468 amp_free(i2c_id);
469 i2c_id = NULL;
470 }
471 if (NULL != new_i2c)
472 {
473 amp_free(new_i2c);
474 new_i2c = NULL;
475 }
476 }
477
478 return (0);
479 }
480 #endif
481
482 #ifdef JSE_HW_ADDON_SPI
483 #include "aos_hal_spi.h"
board_set_spi_default(spi_dev_t * spi_device)484 static void board_set_spi_default(spi_dev_t *spi_device)
485 {
486 if (NULL == spi_device)
487 {
488 return;
489 }
490 spi_device->port = 0;
491 spi_device->priv = NULL;
492 spi_device->config.data_size = 8;
493 spi_device->config.mode = 3;
494 spi_device->config.cs = 0;
495 spi_device->config.role = 1;
496 spi_device->config.serial_len = 0;
497 spi_device->config.firstbit = 0;
498 spi_device->config.t_mode = 1;
499 spi_device->config.freq = 2000000;
500 }
501
board_parse_spi(cJSON * spi,char * id)502 static int8_t board_parse_spi(cJSON *spi, char *id)
503 {
504 int index = 0;
505 int8_t ret = -1;
506 cJSON *port = NULL;
507 cJSON *item = NULL;
508 cJSON *temp = NULL;
509 spi_dev_t device;
510 spi_config_t *config = (spi_config_t *)&device.config;
511 int8_t size = 1;
512
513 if (size <= 0)
514 {
515 return (-1);
516 }
517 while (index < size)
518 {
519 item = spi;
520 index += 1;
521 if (NULL == item)
522 {
523 continue;
524 }
525 port = cJSON_GetObjectItem(item, MARKER_PORT);
526 if (NULL == port || cJSON_Number != port->type)
527 {
528 continue;
529 }
530 board_set_spi_default(&device);
531
532 temp = cJSON_GetObjectItem(item, SPI_MODE);
533 if (NULL != temp && cJSON_String == temp->type)
534 {
535 if (strcmp(temp->valuestring, SPI_MODE_0) == 0) {
536 config->mode = SPI_WORK_MODE_0;
537 }
538
539 else if (strcmp(temp->valuestring, SPI_MODE_1) == 0) {
540 config->mode = SPI_WORK_MODE_1;
541 }
542
543 else if (strcmp(temp->valuestring, SPI_MODE_2) == 0) {
544 config->mode = SPI_WORK_MODE_2;
545 }
546
547 else if (strcmp(temp->valuestring, SPI_MODE_3) == 0) {
548 config->mode = SPI_WORK_MODE_3;
549 }
550 }
551 temp = cJSON_GetObjectItem(item, SPI_FREQ);
552 if (NULL != temp && cJSON_Number == temp->type)
553 {
554 config->freq = temp->valueint;
555 }
556
557 spi_dev_t *new_spi = amp_calloc(1, sizeof(*new_spi));
558 if (NULL == new_spi)
559 {
560 continue;
561 }
562 device.port = port->valueint;
563 char *spi_id = strdup(id);
564 *new_spi = device;
565 ret = board_add_new_item(MODULE_SPI, spi_id, new_spi);
566 if (0 == ret)
567 {
568 continue;
569 }
570 if (NULL != spi_id)
571 {
572 amp_free(spi_id);
573 spi_id = NULL;
574 }
575 if (NULL != new_spi)
576 {
577 amp_free(new_spi);
578 new_spi = NULL;
579 }
580 }
581
582 return (0);
583 }
584 #endif
585
586 #ifdef JSE_HW_ADDON_CAN
587 #include "aos_hal_can.h"
board_set_can_default(can_dev_t * can_device)588 static void board_set_can_default(can_dev_t *can_device)
589 {
590 if (NULL == can_device)
591 {
592 return;
593 }
594 can_device->port = 0;
595 can_device->priv = NULL;
596 can_device->config.baud_rate = CAN_BAUD_500K;
597 can_device->config.ide = CAN_IDE_NORMAL;
598 can_device->config.auto_bus_off = CAN_AUTO_BUS_OFF_ENABLE;
599 can_device->config.auto_retry_transmit = CAN_AUTO_RETRY_TRANSMIT_ENABLE;
600 }
601
board_parse_can(cJSON * can,char * id)602 static int8_t board_parse_can(cJSON *can, char *id)
603 {
604 int index = 0;
605 int8_t ret = -1;
606 cJSON *port = NULL;
607 cJSON *item = NULL;
608 cJSON *temp = NULL;
609 can_dev_t device;
610 can_config_t *config = (can_config_t *)&device.config;
611 int8_t size = 1;
612
613 if (size <= 0)
614 {
615 return (-1);
616 }
617 while (index < size)
618 {
619 item = can;
620 index += 1;
621 if (NULL == item)
622 {
623 continue;
624 }
625 port = cJSON_GetObjectItem(item, MARKER_PORT);
626 if (NULL == port || cJSON_Number != port->type)
627 {
628 continue;
629 }
630 board_set_can_default(&device);
631
632 temp = cJSON_GetObjectItem(item, CAN_BAUD_RATE);
633 if (NULL != temp && cJSON_Number == temp->type)
634 {
635 switch (temp->valueint)
636 {
637 case 1000000:
638 config->baud_rate = CAN_BAUD_1M;
639 break;
640 case 500000:
641 config->baud_rate = CAN_BAUD_500K;
642 break;
643 case 250000:
644 config->baud_rate = CAN_BAUD_250K;
645 break;
646 case 125000:
647 config->baud_rate = CAN_BAUD_125K;
648 break;
649 default:
650 break;
651 }
652 }
653 temp = cJSON_GetObjectItem(item, CAN_IDE);
654 if (NULL != temp && cJSON_String == temp->type)
655 {
656 if (strcmp(temp->valuestring, IDE_NORMAL_CAN) == 0) {
657 config->ide = CAN_IDE_NORMAL;
658 }
659
660 else if (strcmp(temp->valuestring, IDE_EXTEND_CAN) == 0) {
661 config->ide = CAN_IDE_EXTEND;
662 }
663 }
664
665 temp = cJSON_GetObjectItem(item, CAN_AUTO_BUS_OFF);
666 if (NULL != temp && cJSON_String == temp->type)
667 {
668 if (strcmp(temp->valuestring, CAN_DISABLE) == 0) {
669 config->auto_bus_off = CAN_AUTO_BUS_OFF_DISABLE;
670 }
671
672 else if (strcmp(temp->valuestring, CAN_ENABLE) == 0) {
673 config->auto_bus_off = CAN_AUTO_BUS_OFF_ENABLE;
674 }
675 }
676
677 temp = cJSON_GetObjectItem(item, CAN_RETRY_TRANSMIT);
678 if (NULL != temp && cJSON_String == temp->type)
679 {
680 if (strcmp(temp->valuestring, CAN_DISABLE) == 0) {
681 config->auto_retry_transmit = CAN_AUTO_RETRY_TRANSMIT_DISABLE;
682 }
683
684 else if (strcmp(temp->valuestring, CAN_ENABLE) == 0) {
685 config->auto_retry_transmit = CAN_AUTO_RETRY_TRANSMIT_ENABLE;
686 }
687 }
688
689 can_dev_t *new_can = amp_calloc(1, sizeof(*new_can));
690 if (NULL == new_can)
691 {
692 continue;
693 }
694 device.port = port->valueint;
695 char *can_id = strdup(id);
696 *new_can = device;
697 ret = board_add_new_item(MODULE_CAN, can_id, new_can);
698 if (0 == ret)
699 {
700 continue;
701 }
702 if (NULL != can_id)
703 {
704 amp_free(can_id);
705 can_id = NULL;
706 }
707 if (NULL != new_can)
708 {
709 amp_free(new_can);
710 new_can = NULL;
711 }
712 }
713
714 return (0);
715 }
716 #endif
717
718 #ifdef JSE_HW_ADDON_PWM
719 #include "aos_hal_pwm.h"
board_set_pwm_default(pwm_dev_t * pwm_device)720 static void board_set_pwm_default(pwm_dev_t *pwm_device)
721 {
722 if (NULL == pwm_device)
723 {
724 return;
725 }
726 pwm_device->port = 0;
727 pwm_device->priv = NULL;
728 pwm_device->config.freq = 0;
729 pwm_device->config.duty_cycle = 100;
730 }
731
board_parse_pwm(cJSON * pwm,char * id)732 static int8_t board_parse_pwm(cJSON *pwm, char *id)
733 {
734 int index = 0;
735 int8_t ret = -1;
736 cJSON *port = NULL;
737 cJSON *item = NULL;
738 cJSON *temp = NULL;
739 pwm_dev_t device;
740 pwm_config_t *config = (pwm_config_t *)&device.config;
741 int8_t size = 1;
742
743 if (size <= 0)
744 {
745 return (-1);
746 }
747 while (index < size)
748 {
749 item = pwm;
750 index += 1;
751 if (NULL == item)
752 {
753 continue;
754 }
755 port = cJSON_GetObjectItem(item, MARKER_PORT);
756 if (NULL == port || cJSON_Number != port->type)
757 {
758 continue;
759 }
760 board_set_pwm_default(&device);
761 pwm_dev_t *new_pwm = amp_calloc(1, sizeof(*new_pwm));
762 if (NULL == new_pwm)
763 {
764 continue;
765 }
766 device.port = port->valueint;
767 char *pwm_id = strdup(id);
768 *new_pwm = device;
769 ret = board_add_new_item(MODULE_PWM, pwm_id, new_pwm);
770 if (0 == ret)
771 {
772 continue;
773 }
774
775 if (NULL != pwm_id)
776 {
777 amp_free(pwm_id);
778 pwm_id = NULL;
779 }
780 if (NULL != new_pwm)
781 {
782 amp_free(new_pwm);
783 new_pwm = NULL;
784 }
785 }
786
787 return (0);
788 }
789 #endif
790
791 #ifdef JSE_HW_ADDON_ADC
792 #include "aos_hal_adc.h"
board_set_adc_default(adc_dev_t * adc_device)793 static void board_set_adc_default(adc_dev_t *adc_device)
794 {
795 if (NULL == adc_device)
796 {
797 return;
798 }
799 adc_device->port = 0;
800 adc_device->priv = NULL;
801 adc_device->config.sampling_cycle = 12000000;
802 }
803
board_parse_adc(cJSON * adc,char * id)804 static int8_t board_parse_adc(cJSON *adc, char *id)
805 {
806 int index = 0;
807 int8_t ret = -1;
808 cJSON *port = NULL;
809 cJSON *item = NULL;
810 cJSON *temp = NULL;
811 adc_dev_t device;
812 adc_config_t *config = (adc_config_t *)&device.config;
813 int8_t size = 1;
814
815 if (size <= 0)
816 {
817 return (-1);
818 }
819 while (index < size)
820 {
821 item = adc;
822 index += 1;
823 if (NULL == item)
824 {
825 continue;
826 }
827 port = cJSON_GetObjectItem(item, MARKER_PORT);
828 if (NULL == port || cJSON_Number != port->type)
829 {
830 continue;
831 }
832 board_set_adc_default(&device);
833
834 temp = cJSON_GetObjectItem(item, ADC_SAMPLING);
835 if (NULL != temp && cJSON_Number == temp->type)
836 {
837 config->sampling_cycle = temp->valueint;
838 }
839
840 adc_dev_t *new_adc = amp_calloc(1, sizeof(*new_adc));
841 if (NULL == new_adc)
842 {
843 continue;
844 }
845 device.port = port->valueint;
846 char *adc_id = strdup(id);
847 *new_adc = device;
848 ret = board_add_new_item(MODULE_ADC, adc_id, new_adc);
849 if (0 == ret)
850 {
851 continue;
852 }
853
854 if (NULL != adc_id)
855 {
856 amp_free(adc_id);
857 adc_id = NULL;
858 }
859 if (NULL != new_adc)
860 {
861 amp_free(new_adc);
862 new_adc = NULL;
863 }
864 }
865
866 return (0);
867 }
868 #endif
869
870 #ifdef JSE_HW_ADDON_DAC
871 #include "aos_hal_dac.h"
board_set_dac_default(dac_dev_t * dac_device)872 static void board_set_dac_default(dac_dev_t *dac_device)
873 {
874 if (NULL == dac_device)
875 {
876 return;
877 }
878
879 dac_device->port = 0;
880 dac_device->priv = NULL;
881 }
882
board_parse_dac(cJSON * dac,char * id)883 static int8_t board_parse_dac(cJSON *dac, char *id)
884 {
885 int index = 0;
886 int8_t ret = -1;
887 cJSON *port = NULL;
888 cJSON *item = NULL;
889 cJSON *temp = NULL;
890 dac_dev_t device;
891 int8_t size = 1;
892
893 if (size <= 0)
894 {
895 return (-1);
896 }
897 while (index < size)
898 {
899 item = dac;
900 index += 1;
901 if (NULL == item)
902 {
903 continue;
904 }
905 port = cJSON_GetObjectItem(item, MARKER_PORT);
906 if (NULL == port || cJSON_Number != port->type)
907 {
908 continue;
909 }
910 board_set_dac_default(&device);
911 dac_dev_t *new_dac = aos_calloc(1, sizeof(*new_dac));
912 if (NULL == new_dac)
913 {
914 continue;
915 }
916 device.port = port->valueint;
917 char *dac_id = strdup(id);
918 *new_dac = device;
919 ret = board_add_new_item(MODULE_DAC, dac_id, new_dac);
920 if (0 == ret)
921 {
922 continue;
923 }
924
925 if (NULL != dac_id)
926 {
927 aos_free(dac_id);
928 dac_id = NULL;
929 }
930 if (NULL != new_dac)
931 {
932 aos_free(new_dac);
933 new_dac = NULL;
934 }
935 }
936
937 return (0);
938 }
939 #endif
940
941 #ifdef JSE_HW_ADDON_TIMER
942 #include "aos_hal_timer.h"
board_set_timer_default(timer_dev_t * timer_device)943 static void board_set_timer_default(timer_dev_t *timer_device)
944 {
945 if (NULL == timer_device)
946 {
947 return;
948 }
949
950 timer_device->port = 0;
951 timer_device->priv = NULL;
952 timer_device->config.reload_mode = TIMER_RELOAD_MANU;
953 timer_device->config.period = 1000;
954
955 }
956
board_parse_timer(cJSON * timer,char * id)957 static int8_t board_parse_timer(cJSON *timer, char *id)
958 {
959 int index = 0;
960 int8_t ret = -1;
961 cJSON *port = NULL;
962 cJSON *item = NULL;
963 cJSON *temp = NULL;
964 timer_dev_t device;
965 timer_config_t *config = (timer_config_t *)&device.config;
966 int8_t size = 1;
967
968 if (size <= 0)
969 {
970 return (-1);
971 }
972 while (index < size)
973 {
974 item = timer;
975 index += 1;
976 if (NULL == item)
977 {
978 continue;
979 }
980 port = cJSON_GetObjectItem(item, MARKER_PORT);
981 if (NULL == port || cJSON_Number != port->type)
982 {
983 continue;
984 }
985 board_set_timer_default(&device);
986 timer_dev_t *new_timer = amp_calloc(1, sizeof(*new_timer));
987 if (NULL == new_timer)
988 {
989 continue;
990 }
991 device.port = port->valueint;
992 char *timer_id = strdup(id);
993 *new_timer = device;
994 ret = board_add_new_item(MODULE_TIMER, timer_id, new_timer);
995 if (0 == ret)
996 {
997 continue;
998 }
999
1000 if (NULL != timer_id)
1001 {
1002 amp_free(timer_id);
1003 timer_id = NULL;
1004 }
1005 if (NULL != new_timer)
1006 {
1007 amp_free(new_timer);
1008 new_timer = NULL;
1009 }
1010 }
1011
1012 return (0);
1013 }
1014 #endif
1015
1016 #ifdef JSE_ADVANCED_ADDON_AUDIOPLAYER
1017 #include "aos_pcm.h"
board_parse_audio(cJSON * audio,char * id)1018 static int8_t board_parse_audio(cJSON *audio, char *id)
1019 {
1020 int index = 0;
1021 int8_t ret = -1;
1022 cJSON *out_device = NULL;
1023 cJSON *external_pa = NULL;
1024 cJSON *external_pa_pin = NULL;
1025 cJSON *external_pa_delay = NULL;
1026 cJSON *external_pa_active_level = NULL;
1027 cJSON *item = NULL;
1028 cJSON *temp = NULL;
1029 int8_t size = 1;
1030
1031 if (size <= 0)
1032 {
1033 return (-1);
1034 }
1035 while (index < size)
1036 {
1037 item = audio;
1038 index += 1;
1039 if (NULL == item)
1040 {
1041 continue;
1042 }
1043 out_device = cJSON_GetObjectItem(item, OUT_DEVICE);
1044 if (NULL == out_device || cJSON_String != out_device->type)
1045 {
1046 continue;
1047 }
1048 aos_audio_dev_t *new_audio = amp_calloc(1, sizeof(aos_audio_dev_t));
1049 if (NULL == new_audio)
1050 {
1051 continue;
1052 }
1053 if (!strcmp(out_device->valuestring, "speaker"))
1054 {
1055 new_audio->out_device = AOS_SND_DEVICE_OUT_SPEAKER;
1056 }
1057 else if (!strcmp(out_device->valuestring, "receiver"))
1058 {
1059 new_audio->out_device = AOS_SND_DEVICE_OUT_RECEIVER;
1060 }
1061 else if (!strcmp(out_device->valuestring, "headphone"))
1062 {
1063 new_audio->out_device = AOS_SND_DEVICE_OUT_HEADPHONE;
1064 }
1065 else if (!strcmp(out_device->valuestring, "headset"))
1066 {
1067 new_audio->out_device = AOS_SND_DEVICE_OUT_HEADSET;
1068 }
1069 else if (!strcmp(out_device->valuestring, "speaker_and_headphone"))
1070 {
1071 new_audio->out_device = AOS_SND_DEVICE_OUT_SPEAKER_AND_HEADPHONE;
1072 }
1073 else if (!strcmp(out_device->valuestring, "speaker_and_headset"))
1074 {
1075 new_audio->out_device = AOS_SND_DEVICE_OUT_SPEAKER_AND_HEADSET;
1076 }
1077 else
1078 {
1079 new_audio->out_device = AOS_SND_DEVICE_OUT_SPEAKER;
1080 }
1081
1082 external_pa = cJSON_GetObjectItem(item, EXTERNAL_PA);
1083 if (NULL == external_pa)
1084 {
1085 new_audio->external_pa = 0;
1086 }
1087 else
1088 {
1089 if (cJSON_String != external_pa->type)
1090 {
1091 new_audio->external_pa = 0;
1092 }
1093 else
1094 {
1095 if (!strcmp(external_pa->valuestring, "enable"))
1096 {
1097 new_audio->external_pa = 1;
1098 }
1099 else
1100 {
1101 new_audio->external_pa = 0;
1102 }
1103 }
1104 }
1105
1106 external_pa_pin = cJSON_GetObjectItem(item, EXTERNAL_PA_PIN);
1107 if (NULL == external_pa_pin)
1108 {
1109 new_audio->external_pa_pin = -1;
1110 }
1111 else
1112 {
1113 if (cJSON_Number != external_pa_pin->type)
1114 {
1115 new_audio->external_pa_pin = -1;
1116 }
1117 else
1118 {
1119 new_audio->external_pa_pin = external_pa_pin->valueint;
1120 }
1121 }
1122
1123 external_pa_delay = cJSON_GetObjectItem(item, EXTERNAL_PA_DELAY);
1124 if (NULL == external_pa_delay)
1125 {
1126 new_audio->external_pa_delay_ms = 0;
1127 }
1128 else
1129 {
1130 if (cJSON_Number != external_pa_delay->type)
1131 {
1132 new_audio->external_pa_delay_ms = 0;
1133 }
1134 else
1135 {
1136 new_audio->external_pa_delay_ms = external_pa_delay->valueint;
1137 }
1138 }
1139
1140 external_pa_active_level = cJSON_GetObjectItem(item, EXTERNAL_PA_ACTIVE);
1141 if (NULL == external_pa_active_level)
1142 {
1143 new_audio->external_pa_active_high = 1;
1144 }
1145 else
1146 {
1147 if (cJSON_Number != external_pa_active_level->type)
1148 {
1149 new_audio->external_pa_active_high = 1;
1150 }
1151 else
1152 {
1153 new_audio->external_pa_active_high = external_pa_active_level->valueint;
1154 }
1155 }
1156
1157 char *audio_id = strdup(id);
1158 ret = board_add_new_item(MODULE_AUDIO, audio_id, new_audio);
1159 if (0 == ret)
1160 {
1161 continue;
1162 }
1163
1164 if (NULL != audio_id)
1165 {
1166 amp_free(audio_id);
1167 audio_id = NULL;
1168 }
1169 if (NULL != new_audio)
1170 {
1171 amp_free(new_audio);
1172 new_audio = NULL;
1173 }
1174 }
1175
1176 return (0);
1177 }
1178 #endif
1179
board_get_json_buff(const char * json_path)1180 static char *board_get_json_buff(const char *json_path)
1181 {
1182 void *json_data = NULL;
1183 uint32_t len = 0;
1184 int json_fd = -1;
1185
1186 if (NULL == json_path)
1187 {
1188 return (NULL);
1189 }
1190
1191 json_fd = aos_open(json_path, O_RDONLY);
1192 if (json_fd < 0) {
1193 amp_debug(MOD_STR, "aos open fail");
1194 return (NULL);
1195 }
1196 // amp_debug(MOD_STR, "jse_lseek");
1197 len = aos_lseek(json_fd, 0, HAL_SEEK_END);
1198 amp_warn(MOD_STR, "%s len %u", json_path, len);
1199 json_data = amp_calloc(1, sizeof(char) * (len + 1));
1200 if (NULL == json_data)
1201 {
1202 aos_close(json_fd);
1203 amp_debug(MOD_STR, "jse_close");
1204 return (NULL);
1205 }
1206 aos_lseek(json_fd, 0, HAL_SEEK_SET);
1207 // amp_debug(MOD_STR, "jse_read");
1208 aos_read(json_fd, json_data, len);
1209 // amp_debug(MOD_STR, "jse_read, data: %s", json_data);
1210 aos_close(json_fd);
1211 return json_data;
1212 }
1213
1214 static parse_json_t g_parse_json[] = {
1215 #ifdef JSE_HW_ADDON_UART
1216 {MARKER_UART, MODULE_UART, board_parse_uart},
1217 #endif
1218
1219 #ifdef JSE_HW_ADDON_GPIO
1220 {MARKER_GPIO, MODULE_GPIO, board_parse_gpio},
1221 #endif
1222
1223 #ifdef JSE_HW_ADDON_PWM
1224 {MARKER_PWM, MODULE_PWM, board_parse_pwm},
1225 #endif
1226
1227 #ifdef JSE_HW_ADDON_I2C
1228 {MARKER_I2C, MODULE_I2C, board_parse_i2c},
1229 #endif
1230
1231 #ifdef JSE_HW_ADDON_SPI
1232 {MARKER_SPI, MODULE_SPI, board_parse_spi},
1233 #endif
1234
1235 #ifdef JSE_HW_ADDON_CAN
1236 {MARKER_CAN, MODULE_CAN, board_parse_can},
1237 #endif
1238
1239 #ifdef JSE_HW_ADDON_ADC
1240 {MARKER_ADC, MODULE_ADC, board_parse_adc},
1241 #endif
1242
1243 #ifdef JSE_HW_ADDON_DAC
1244 {MARKER_DAC, MODULE_DAC, board_parse_dac},
1245 #endif
1246
1247 #ifdef JSE_HW_ADDON_TIMER
1248 {MARKER_TIMER, MODULE_TIMER, board_parse_timer},
1249 #endif
1250
1251 #ifdef JSE_ADVANCED_ADDON_AUDIOPLAYER
1252 {MARKER_AUDIO, MODULE_AUDIO, board_parse_audio},
1253 #endif
1254 {NULL, MODULE_NUMS, NULL},
1255 };
1256
board_parse_json_buff(const char * json_buff)1257 static int32_t board_parse_json_buff(const char *json_buff)
1258 {
1259 cJSON *root = NULL;
1260 cJSON *page = NULL, *pages = NULL;
1261 cJSON *io = NULL;
1262 cJSON *audio = NULL;
1263 cJSON *debug = NULL;
1264 cJSON *repl = NULL;
1265 cJSON *version = NULL;
1266 cJSON *item = NULL;
1267 cJSON *child = NULL;
1268 parse_json_t *parser_handle = NULL;
1269
1270 if (NULL == json_buff)
1271 {
1272 return -1;
1273 }
1274 root = cJSON_Parse(json_buff);
1275 if (NULL == root)
1276 {
1277 return -1;
1278 }
1279
1280 /* debugLevel configuration */
1281 if((debug = cJSON_GetObjectItem(root, APP_CONFIG_DEBUG)) != NULL) {
1282 /* parsing debugLevel configuration */
1283 if (!cJSON_IsString(debug)) {
1284 amp_error(MOD_STR, "debugLevel not string");
1285 goto parse_end;
1286 }
1287
1288 amp_debug(MOD_STR, "get debugLevel:%s", debug->valuestring);
1289 if(strcmp(debug->valuestring, "DEBUG") == 0) {
1290 aos_set_log_level(LOG_DEBUG);
1291 }
1292 else if(strcmp(debug->valuestring, "INFO") == 0) {
1293 aos_set_log_level(LOG_INFO);
1294 }
1295 else if(strcmp(debug->valuestring, "WARN") == 0) {
1296 aos_set_log_level(LOG_WARNING);
1297
1298 }
1299 else if(strcmp(debug->valuestring, "ERROR") == 0) {
1300 aos_set_log_level(LOG_ERR);
1301
1302 }
1303 else if(strcmp(debug->valuestring, "FATAL") == 0) {
1304 aos_set_log_level(LOG_CRIT);
1305 }
1306 else {
1307 amp_debug(MOD_STR, "debugLevel error, set to default: 'ERROR'");
1308 aos_set_log_level(LOG_ERR);
1309 }
1310 }
1311 else {
1312 amp_debug(MOD_STR, "No debugLevel configuration in app.json, set to default: 'ERROR'");
1313 }
1314
1315 /* page configuration */
1316 if((pages = cJSON_GetObjectItem(root, APP_CONFIG_PAGES)) != NULL) {
1317 #if 0
1318 /* parsing io configuration */
1319 if(!cJSON_IsArray(pages)) {
1320 amp_error(MOD_STR, "Pages entries need array");
1321 goto parse_end;
1322 }
1323
1324 dlist_init(&g_pages_list);
1325 cJSON_ArrayForEach(page, pages) {
1326 if (!cJSON_IsString(page)) {
1327 amp_error(MOD_STR, "page not string");
1328 goto parse_end;
1329 }
1330
1331 amp_debug(MOD_STR, "get page:%s", page->valuestring);
1332
1333 /* add page to dlink */
1334 page_entry_t *page_entry = amp_malloc(sizeof(page_entry_t));
1335 page_entry->page = strdup(page->valuestring); /* don't forget to free */
1336 dlist_add_tail(&page_entry->node, &g_pages_list);
1337 }
1338 #else
1339 #ifdef JSE_ADVANCED_ADDON_UI
1340 app_run_flag = 1;
1341 page_config_parse();
1342 #endif
1343 #endif
1344 }
1345 else {
1346 amp_debug(MOD_STR, "No pages configuration in app.json");
1347 }
1348
1349 /* repl configuration */
1350 if((repl = cJSON_GetObjectItem(root, APP_CONFIG_REPL)) != NULL) {
1351 /* parsing debugLevel configuration */
1352 if (!cJSON_IsString(repl)) {
1353 amp_error(MOD_STR, "repl not string");
1354 goto parse_end;
1355 }
1356
1357 amp_debug(MOD_STR, "get app repl config is:%s", repl->valuestring);
1358 if (strcmp(repl->valuestring, "disable") == 0) {
1359 g_repl_config = 0;
1360 } else if (strcmp(repl->valuestring, "enable") == 0) {
1361 g_repl_config = 1;
1362 } else {
1363 amp_debug(MOD_STR, "repl configuration is wrong, set to default: 'enable'");
1364 g_repl_config = 1;
1365 }
1366 }
1367 else {
1368 amp_debug(MOD_STR, "No repl configuration in app.json, set to default: 'enable'");
1369 }
1370
1371 /* net configuration */
1372 /* TODO */
1373
1374 /* io configuration */
1375 if((io = cJSON_GetObjectItem(root, APP_CONFIG_IO)) != NULL) {
1376 /* parsing io configuration */
1377 child = io->child;
1378 while (NULL != child) {
1379 item = cJSON_GetObjectItem(child, MARKER_ID);
1380 if (NULL == item || cJSON_String != item->type) {
1381 child = child->next;
1382 continue;
1383 }
1384 parser_handle = &g_parse_json[0];
1385 while (NULL != parser_handle->marker_name) {
1386 if (0 == strcmp(item->valuestring, parser_handle->marker_name)) {
1387 parser_handle->fn(child, child->string);
1388 }
1389 parser_handle += 1;
1390 }
1391 child = child->next;
1392 }
1393 }
1394 else {
1395 amp_warn(MOD_STR, "No io configuration in app.json");
1396 }
1397
1398 /* audio configuration */
1399 if((audio = cJSON_GetObjectItem(root, APP_CONFIG_AUDIO)) != NULL) {
1400 /* parsing audio configuration */
1401 item = cJSON_GetObjectItem(audio, MARKER_ID);
1402 if (NULL == item || cJSON_String != item->type) {
1403 amp_warn(MOD_STR, "audio marker invalid");
1404 } else {
1405 parser_handle = &g_parse_json[0];
1406 while (NULL != parser_handle->marker_name) {
1407 if (0 == strcmp(item->valuestring, parser_handle->marker_name)) {
1408 parser_handle->fn(audio, audio->string);
1409 break;
1410 }
1411 parser_handle += 1;
1412 }
1413 }
1414 }
1415 else {
1416 amp_debug(MOD_STR, "No audio configuration in app.json");
1417 }
1418
1419 /* version configuration */
1420 if((version = cJSON_GetObjectItem(root, APP_CONFIG_VERSION)) != NULL) {
1421 if (!cJSON_IsString(version)) {
1422 amp_error(MOD_STR, "version not string");
1423 goto parse_end;
1424 }
1425
1426 amp_debug(MOD_STR, "get app version is: %s", version->valuestring);
1427 #ifndef HAASUI_AMP_BUILD
1428 aos_userjs_version_set(version->valuestring);
1429 #endif
1430 }
1431 else {
1432 amp_debug(MOD_STR, "No version info in app.json");
1433 }
1434
1435 cJSON_Delete(root);
1436 return 0;
1437
1438 parse_end:
1439 cJSON_Delete(root);
1440 return -1;
1441 }
1442
board_get_items(addon_module_m module,item_handle_t * handle,const char * name_id)1443 static void *board_get_items(addon_module_m module, item_handle_t *handle,
1444 const char *name_id)
1445 {
1446 board_mgr_t *mgr_handle = board_get_handle();
1447 board_item_t *item = NULL;
1448 if (NULL == handle && NULL == name_id)
1449 {
1450 return (NULL);
1451 }
1452 uint32_t i = 0;
1453 for (i = 0; i < mgr_handle->item_size; ++i)
1454 {
1455 item = mgr_handle->item[i];
1456 if (module != item->module)
1457 {
1458 continue;
1459 }
1460 if (NULL != handle && item->handle.handle != handle->handle)
1461 {
1462 continue;
1463 }
1464 if (NULL != name_id && 0 != strcmp(item->name_id, name_id))
1465 {
1466 continue;
1467 }
1468 return (item);
1469 }
1470
1471 return (NULL);
1472 }
1473
board_add_new_item(addon_module_m module,char * name_id,void * node)1474 static int8_t board_add_new_item(addon_module_m module, char *name_id,
1475 void *node)
1476 {
1477 board_item_t *item = NULL;
1478 board_mgr_t *mgr_handle = board_get_handle();
1479 if (NULL == name_id || NULL == node)
1480 return (-1);
1481 if (NULL != board_get_items(module, NULL, name_id))
1482 {
1483 return (-1);
1484 }
1485 board_item_t *new_item = amp_calloc(1, sizeof(*new_item));
1486 if (NULL == new_item)
1487 {
1488 return (-1);
1489 }
1490 void *addr = amp_realloc(
1491 mgr_handle->item, sizeof(board_item_t *) * (mgr_handle->item_size + 1));
1492 if (NULL == addr)
1493 {
1494 goto out;
1495 }
1496
1497 new_item->module = module;
1498 new_item->name_id = name_id;
1499 new_item->handle.handle = (void*)new_item;
1500 new_item->node = node;
1501 new_item->status = 0;
1502 mgr_handle->item = addr;
1503 mgr_handle->item[mgr_handle->item_size] = new_item;
1504 mgr_handle->item_size += 1;
1505
1506 return (0);
1507 out:
1508 if (NULL != new_item)
1509 {
1510 amp_free(new_item);
1511 new_item = NULL;
1512 }
1513 return (-1);
1514 }
1515
board_attach_item(addon_module_m module,const char * name_id,item_handle_t * out)1516 int8_t board_attach_item(addon_module_m module, const char *name_id,
1517 item_handle_t *out)
1518 {
1519 board_item_t *item = NULL;
1520 if (NULL == name_id)
1521 {
1522 return (-1);
1523 }
1524 item = board_get_items(module, NULL, name_id);
1525 if (NULL == item || 1 == item->status)
1526 {
1527 return (-1);
1528 }
1529 item->status = 1;
1530 *out = item->handle;
1531
1532 return (0);
1533 }
1534
board_disattach_item(addon_module_m module,item_handle_t * handle)1535 int8_t board_disattach_item(addon_module_m module, item_handle_t *handle)
1536 {
1537 board_item_t *item = NULL;
1538 if (NULL == handle)
1539 {
1540 return (-1);
1541 }
1542 item = board_get_items(module, handle, NULL);
1543 if (NULL == item)
1544 {
1545 return (-1);
1546 }
1547 item->status = 0;
1548
1549 return (0);
1550 }
1551
board_check_attach_status(addon_module_m module,item_handle_t * handle)1552 int8_t board_check_attach_status(addon_module_m module, item_handle_t *handle)
1553 {
1554 board_item_t *item = NULL;
1555 if (NULL == handle)
1556 {
1557 return (0);
1558 }
1559 item = board_get_items(module, handle, NULL);
1560 if (NULL == item)
1561 {
1562 return (0);
1563 }
1564
1565 return (item->status);
1566 }
1567
board_get_node_by_name(addon_module_m module,const char * name_id)1568 void *board_get_node_by_name(addon_module_m module, const char *name_id)
1569 {
1570 board_item_t *item = NULL;
1571 if (NULL == name_id)
1572 {
1573 return (NULL);
1574 }
1575 item = board_get_items(module, NULL, name_id);
1576 if (NULL == item || 0 == item->status)
1577 {
1578 return (NULL);
1579 }
1580
1581 return (item->node);
1582 }
1583
board_get_node_by_handle(addon_module_m module,item_handle_t * handle)1584 void *board_get_node_by_handle(addon_module_m module, item_handle_t *handle)
1585 {
1586 board_item_t *item = NULL;
1587 if (NULL == handle)
1588 return (NULL);
1589 item = board_get_items(module, handle, NULL);
1590 if (NULL == item || 0 == item->status)
1591 {
1592 return (NULL);
1593 }
1594
1595 return (item->node);
1596 }
1597
board_mgr_init(const char * json_path)1598 int32_t board_mgr_init(const char *json_path)
1599 {
1600 int32_t ret = -1;
1601 char *json = NULL;
1602
1603 memset(&g_board_mgr, 0x00, sizeof(g_board_mgr));
1604 json = board_get_json_buff(json_path);
1605 if (NULL == json)
1606 {
1607 amp_debug(MOD_STR, "default board config is null");
1608 return ret;
1609 }
1610 // return 0;
1611 ret = board_parse_json_buff(json);
1612 amp_free(json);
1613 json = NULL;
1614
1615 return ret;
1616 }
1617
board_load_drivers(const char * driver)1618 int8_t board_load_drivers(const char *driver)
1619 {
1620 char *p = (char *)driver;
1621 char *index = NULL;
1622 char *json = NULL;
1623 char *new_driver = NULL;
1624 int32_t len = -1;
1625 int8_t ret = -1;
1626 if (NULL == driver)
1627 {
1628 return (-1);
1629 }
1630 len = strlen(driver);
1631 if (len < 8)
1632 {
1633 return (-1);
1634 }
1635 if (0 != strncmp(driver + len - 3, ".js", 3))
1636 {
1637 return (-1);
1638 }
1639 p = p + strlen("/spiffs/");
1640 index = (char *)driver + len - 1;
1641 while (index > p)
1642 {
1643 if (*index == '/')
1644 {
1645 break;
1646 }
1647 index -= 1;
1648 }
1649 if (index <= p)
1650 {
1651 return (-1);
1652 }
1653 new_driver = amp_calloc(1, sizeof(char) * (index - driver + 16));
1654 if (NULL == new_driver)
1655 {
1656 return (-1);
1657 }
1658 memmove(new_driver, driver, index - driver + 1);
1659 memmove(new_driver + (index - driver + 1), DRIVER_NAME,
1660 strlen(DRIVER_NAME));
1661
1662 amp_debug(MOD_STR, "%s%d, new_driver = %s ", __FUNCTION__, __LINE__,
1663 new_driver);
1664
1665 json = board_get_json_buff(new_driver);
1666 if (NULL == json)
1667 {
1668 goto out;
1669 }
1670 ret = board_parse_json_buff(json);
1671 out:
1672 if (NULL != new_driver)
1673 {
1674 amp_free(new_driver);
1675 new_driver = NULL;
1676 }
1677 if (NULL != json)
1678 {
1679 amp_free(json);
1680 json = NULL;
1681 }
1682 return (ret);
1683 }
1684