int main(void) { bl_init(); usart_start(); spi_start(); if (usbd_start()) { led_blink(LED_ACTIVITY, LED_STATE_RAPID); for (;;); } /* HW initialized */ led_on(LED_ACTIVITY); bl_dbg("Bootloader started."); // test_fatfs(); if (usb_connect()) { led_on(LED_USB); bl_dbg("USB connected."); bl_listen(); } else led_off(LED_USB); bl_dbg("No USB."); jump_to_app(APP_LOAD_ADDRESS); return 0; }
bl_t bl_create(bool srv, const char *path, void (*fun)(int, const char *, va_list)) { bl_t b = calloc(1, sizeof(*b)); if (b == NULL) goto out; b->b_fun = fun == NULL ? vsyslog : fun; b->b_fd = -1; b->b_connected = -1; BL_INIT(b); memset(&b->b_sun, 0, sizeof(b->b_sun)); b->b_sun.sun_family = AF_LOCAL; #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN b->b_sun.sun_len = sizeof(b->b_sun); #endif strlcpy(b->b_sun.sun_path, path ? path : _PATH_BLSOCK, sizeof(b->b_sun.sun_path)); bl_init(b, srv); return b; out: free(b); bl_log(fun, LOG_ERR, "%s: malloc failed (%m)", __func__); return NULL; }
bl* bl_new(int blocksize, int datasize) { bl* rtn; rtn = malloc(sizeof(bl)); if (!rtn) { printf("Couldn't allocate memory for a bl.\n"); return NULL; } bl_init(rtn, blocksize, datasize); return rtn; }
int main(int argc, char** argv) { bl_argc = argc; bl_argv = argv; drvlfb_system = drvlfb_new_system(DRVLFB_FB0_DEVICE_NAME, 640, 480); bl_init(); blMain(); bl_exit(); return 0; }
// Return 0 if no errors // Returns 1 if function need to be called again static int check_errors(int thd_nb, int code) { printf("[THD%d]Error code = %d\n", thd_nb, code); if (code != BL_NO_ERROR) { printf("[THD%d]error count: %d\n", thd_nb, error_cnt); if (error_cnt < RETRY_MAX) error_cnt++; else exit(-1); switch(code) { case BL_NO_ERROR: case BL_ALREADY_CONNECTED_ERROR: case BL_MTU_ALREADY_EXCHANGED_ERROR: case BL_UNICITY_ERROR: case BL_NOT_NOTIFIABLE_ERROR: case BL_NOT_INDICABLE_ERROR: /* For test */ case EINVAL: case BL_REQUEST_FAIL_ERROR: error_cnt = 0; printf("[THD%d] Going on anyway\n", thd_nb); return 0; case BL_NO_CTX_ERROR: while(bl_init(NULL)) { printf("[THD%d] error count: %d\n", thd_nb, error_cnt); if (error_cnt < RETRY_MAX) error_cnt++; else exit(-1); } return 1; break; case BL_RECONNECTION_NEEDED_ERROR: case BL_DISCONNECTED_ERROR: case BL_NO_CALLBACK_ERROR: for(;;) { printf("[THD%d] Next try in 10 seconds\n", thd_nb); sleep(10); printf("[THD%d] Try to reconnect\n", thd_nb); int ret = bl_connect(&dev_ctx); if ((ret != BL_NO_ERROR) && (ret != BL_ALREADY_CONNECTED_ERROR) && (ret != BL_NOT_NOTIFIABLE_ERROR )) { printf("[THD%d] ERROR <%d>\n", thd_nb, ret); if (error_cnt < RETRY_MAX) { error_cnt++; printf("[THD%d] Error count: %d\n", thd_nb, error_cnt); } else exit(-1); } else { printf("[THD%d] Reconnected\n", thd_nb); return 1; } } break; case BL_SEND_REQUEST_ERROR: return 1; break; default: // FATAL ERRROS case BL_MALLOC_ERROR: case BL_LE_ONLY_ERROR: case BL_MISSING_ARGUMENT_ERROR: case BL_PROTOCOL_ERROR: exit(-1); } } else { error_cnt = 0; return 0; } }
static int get_ble_tree(const int thd_nb, char *mac, const char *file_path) { int ret_int; GError *gerr = NULL; GSList *bl_primary_list = NULL; GSList *bl_included_list = NULL; GSList *bl_char_list = NULL; GSList *bl_desc_list = NULL; bl_value_t *bl_value = NULL; FILE *file = fopen(file_path, "w"); if (!file) return -1; // Initialisation bl_init(&gerr); if (check_gerrors(thd_nb, gerr)) { printf("[THD%d] ERROR: Unable to initalise BlueLib\n", thd_nb); return -1; } dev_init(&dev_ctx, NULL, mac, NULL, 0, TEST_SEC_LEVEL); do { ret_int = bl_connect(&dev_ctx); } while (check_errors(thd_nb, ret_int)); if (ret_int) return -1; do { bl_value = bl_read_char(&dev_ctx, GATT_CHARAC_DEVICE_NAME_STR, NULL, &gerr); } while(check_gerrors(thd_nb, gerr)); printf("[THD%d] In progress\n", thd_nb); if (bl_value) { char device_name_str[bl_value->data_size + 1]; memcpy(device_name_str, bl_value->data, bl_value->data_size); device_name_str[bl_value->data_size] = '\0'; fprintf(file, "Device name: %s\n", device_name_str); bl_value_free(bl_value); } else { printf("[THD%d] Impossible to retrieve the name of the device \n", thd_nb); goto disconnect; } fprintf(file, "Handle |\n"); do { bl_primary_list = bl_get_all_primary(&dev_ctx, NULL, &gerr); } while (check_gerrors(thd_nb, gerr)); printf("[THD%d].", thd_nb); if (bl_primary_list) { for (GSList *lp = bl_primary_list; lp; lp = lp->next) { printf("."); bl_primary_t *bl_primary = lp->data; bl_primary_fprint(file, lp->data); // Get all included in the primary service do { bl_included_list = bl_get_included(&dev_ctx, bl_primary, &gerr); } while (check_gerrors(thd_nb, gerr)); if (bl_included_list) { bl_included_list_fprint(file, bl_included_list); fprintf(file, " |\n"); bl_included_list_free(bl_included_list); } // Get all characteristics in the primary service do { bl_char_list = bl_get_all_char_in_primary(&dev_ctx, bl_primary, &gerr); } while (check_gerrors(thd_nb, gerr)); if (bl_char_list) { for (GSList *lc = bl_char_list; lc; lc = lc->next) { putchar('.'); bl_char_t *bl_char = lc->data; bl_char_fprint(file, bl_char); // Get all descriptors of the characteristic { bl_char_t *next_bl_char = NULL; if (lc->next) next_bl_char = lc->next->data; do { bl_desc_list = bl_get_all_desc_by_char(&dev_ctx, bl_char, next_bl_char, bl_primary, &gerr); } while (check_gerrors(thd_nb, gerr)); } if (bl_desc_list) bl_desc_list_fprint(file, bl_desc_list); bl_desc_list_free(bl_desc_list); if (lc->next) fprintf(file, " | |\n"); } bl_char_list_free(bl_char_list); } if (lp->next) fprintf(file, " |\n"); } bl_primary_list_free(bl_primary_list); } printf("[THD%d] All done!\n", thd_nb); disconnect: if (file) fclose(file); printf("[THD%d] Disconnecting\n", thd_nb); bl_disconnect(&dev_ctx); bl_stop(); return 0; }
int bl_send(bl_t b, bl_type_t e, int pfd, const struct sockaddr *sa, socklen_t slen, const char *ctx) { struct msghdr msg; struct iovec iov; union { char ctrl[CMSG_SPACE(sizeof(int))]; uint32_t fd; } ua; struct cmsghdr *cmsg; union { bl_message_t bl; char buf[512]; } ub; size_t ctxlen, tried; #define NTRIES 5 ctxlen = strlen(ctx); if (ctxlen > 128) ctxlen = 128; iov.iov_base = ub.buf; iov.iov_len = sizeof(bl_message_t) + ctxlen; ub.bl.bl_len = (uint32_t)iov.iov_len; ub.bl.bl_version = BL_VERSION; ub.bl.bl_type = (uint32_t)e; if (bl_getsock(b, &ub.bl.bl_ss, sa, slen, ctx) == -1) return -1; ub.bl.bl_salen = slen; memcpy(ub.bl.bl_data, ctx, ctxlen); msg.msg_name = NULL; msg.msg_namelen = 0; msg.msg_iov = &iov; msg.msg_iovlen = 1; msg.msg_flags = 0; msg.msg_control = ua.ctrl; msg.msg_controllen = sizeof(ua.ctrl); cmsg = CMSG_FIRSTHDR(&msg); cmsg->cmsg_len = CMSG_LEN(sizeof(int)); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; memcpy(CMSG_DATA(cmsg), &pfd, sizeof(pfd)); tried = 0; again: if (bl_init(b, false) == -1) return -1; if ((sendmsg(b->b_fd, &msg, 0) == -1) && tried++ < NTRIES) { bl_reset(b, false); goto again; } return tried >= NTRIES ? -1 : 0; }
int main(void) { // clock init clock_init(); // two timers for tcp/ip timer_set(&periodic_timer, CLOCK_SECOND / 2); /* 0.5s */ timer_set(&arp_timer, CLOCK_SECOND * 10); /* 10s */ diag_init(); // ethernet init tapdev_init(); // Initialize the uIP TCP/IP stack. uip_init(); uip_ipaddr(ipaddr, MYIP1,MYIP2,MYIP3,MYIP4); uip_sethostaddr(ipaddr); uip_ipaddr(ipaddr, DRTR1,DRTR2,DRTR3,DRTR4); uip_setdraddr(ipaddr); uip_ipaddr(ipaddr, SMSK1, SMSK2, SMSK3, SMSK4); uip_setnetmask(ipaddr); bl_init(); while(1) { diag_appcall(); bl_appcall(); /* receive packet and put in uip_buf */ uip_len = tapdev_read(uip_buf); if(uip_len > 0) /* received packet */ { if(BUF->type == htons(UIP_ETHTYPE_IP)) /* IP packet */ { uip_arp_ipin(); uip_input(); /* If the above function invocation resulted in data that should be sent out on the network, the global variable uip_len is set to a value > 0. */ if(uip_len > 0) { uip_arp_out(); tapdev_send(uip_buf,uip_len); } } else if(BUF->type == htons(UIP_ETHTYPE_ARP)) /*ARP packet */ { uip_arp_arpin(); /* If the above function invocation resulted in data that should be sent out on the network, the global variable uip_len is set to a value > 0. */ if(uip_len > 0) { tapdev_send(uip_buf,uip_len); /* ARP ack*/ } } } else if(timer_expired(&periodic_timer)) /* no packet but periodic_timer time out (0.5s)*/ { timer_reset(&periodic_timer); for(i = 0; i < UIP_CONNS; i++) { uip_periodic(i); /* If the above function invocation resulted in data that should be sent out on the network, the global variable uip_len is set to a value > 0. */ if(uip_len > 0) { uip_arp_out(); tapdev_send(uip_buf,uip_len); } } #if UIP_UDP for(i = 0; i < UIP_UDP_CONNS; i++) { uip_udp_periodic(i); /* If the above function invocation resulted in data that should be sent out on the network, the global variable uip_len is set to a value > 0. */ if(uip_len > 0) { uip_arp_out(); tapdev_send(uip_buf,uip_len); } } #endif /* UIP_UDP */ /* Call the ARP timer function every 10 seconds. */ if(timer_expired(&arp_timer)) { timer_reset(&arp_timer); uip_arp_timer(); } } } return 0 ; }