void bt_init ( void ) { sys_init(); mem_init(); memp_init(); pbuf_init(); UART2PrintString("mem mgmt initialized\r\n"); lwbt_memp_init(); phybusif_init(""); cb = malloc(sizeof(struct phybusif_cb)); phybusif_reset(cb); if(hci_init() != ERR_OK) { UART2PrintString("HCI initialization failed!\r\n"); return; } l2cap_init(); sdp_init(); rfcomm_init(); UART2PrintString("Bluetooth initialized.\r\n"); bt_spp_start(); UART2PrintString("Applications started.\r\n"); }
/*-----------------------------------------------------------------------------------*/ err_t phybusif_input(struct phybusif_cb *cb) { unsigned char c; unsigned char n; while((n = read(fd,&c,1))) { switch(cb->state) { case W4_PACKET_TYPE: switch(c) { case HCI_ACL_DATA_PACKET: cb->state = W4_ACL_HDR; break; case HCI_EVENT_PACKET: cb->state = W4_EVENT_HDR; break; default: LWIP_DEBUGF(PHYBUSIF_DEBUG, ("phybusif_input: Unknown packet type\n")); break; } break; case W4_EVENT_HDR: ((u8_t *)cb->q->payload)[cb->recvd] = c; cb->tot_recvd++; cb->recvd++; if(cb->recvd == HCI_EVENT_HDR_LEN) { cb->evhdr = cb->p->payload; pbuf_header(cb->p, -HCI_EVENT_HDR_LEN); cb->recvd = cb->tot_recvd = 0; if(cb->evhdr->len > 0) { cb->state = W4_EVENT_PARAM; } else { hci_event_input(cb->p); /* Handle incoming event */ pbuf_free(cb->p); phybusif_reset(cb); return ERR_OK; /* Since there most likley won't be any more data in the input buffer */ } } break; case W4_EVENT_PARAM: ((u8_t *)cb->q->payload)[cb->recvd] = c; cb->tot_recvd++; cb->recvd++; if(cb->recvd == cb->q->len) { /* Pbuf full. alloc and add new tail to chain */ cb->recvd = 0; if((cb->q = pbuf_alloc(PBUF_RAW, PBUF_POOL_BUFSIZE, PBUF_POOL)) == NULL) { LWIP_DEBUGF(PHYBUSIF_DEBUG, ("phybusif_input: Could not allocate memory for event parameter pbuf\n")); return ERR_MEM; /* Could not allocate memory for pbuf */ } pbuf_chain(cb->p, cb->q); pbuf_free(cb->q); } if(cb->tot_recvd == cb->evhdr->len) { hci_event_input(cb->p); /* Handle incoming event */ pbuf_free(cb->p); phybusif_reset(cb); return ERR_OK; /* Since there most likley won't be any more data in the input buffer */ } break; case W4_ACL_HDR: ((u8_t *)cb->q->payload)[cb->recvd] = c; cb->tot_recvd++; cb->recvd++; if(cb->recvd == HCI_ACL_HDR_LEN) { cb->aclhdr = cb->p->payload; pbuf_header(cb->p, -HCI_ACL_HDR_LEN); cb->recvd = cb->tot_recvd = 0; if(cb->aclhdr->len > 0) { cb->state = W4_ACL_DATA; } else { LWIP_DEBUGF(PHYBUSIF_DEBUG, ("phybusif_reset: Forward Empty ACL packet to higher layer\n")); hci_acl_input(cb->p); /* Handle incoming ACL data */ phybusif_reset(cb); return ERR_OK; /* Since there most likley won't be any more data in the input buffer */ } } break; case W4_ACL_DATA: ((u8_t *)cb->q->payload)[cb->recvd] = c; cb->tot_recvd++; cb->recvd++; if(cb->recvd == cb->q->len) { /* Pbuf full. alloc and add new tail to chain */ cb->recvd = 0; if((cb->q = pbuf_alloc(PBUF_RAW, PBUF_POOL_BUFSIZE, PBUF_POOL)) == NULL) { LWIP_DEBUGF(PHYBUSIF_DEBUG, ("phybusif_input: Could not allocate memory for ACL data pbuf\n")); return ERR_MEM; /* Could not allocate memory for pbuf */ } pbuf_chain(cb->p, cb->q); pbuf_free(cb->q); } if(cb->tot_recvd == cb->aclhdr->len) { LWIP_DEBUGF(PHYBUSIF_DEBUG, ("phybusif_input: Forward ACL packet to higher layer\n")); hci_acl_input(cb->p); /* Handle incoming ACL data */ phybusif_reset(cb); return ERR_OK; /* Since there most likley won't be any more data in the input buffer */ } break; default: LWIP_DEBUGF(PHYBUSIF_DEBUG, ("phybusif_input: Unknown state\n\n")); break; } } return ERR_OK; }
/*-----------------------------------------------------------------------------------*/ void maintask(void) { struct phybusif_cb *cb; CLKBLK ks_clk *tmr; u8_t bt_timer = 0; u16_t http_timer = 0; u16_t dma0bsz = TCR0+1; /* DMA 0 Buf size */ u8_t bt_ip_timer = 0; mem_init(); memp_init(); pbuf_init(); netif_init(); ip_init(); tcp_init(); sio_print("TCP/IP initialized.\n"); lwbt_memp_init(); phybusif_init(); if(hci_init() != ERR_OK) { sio_print("HCI initialization failed!"); } l2cap_init(); sdp_init(); rfcomm_init(); ppp_init(); sio_print("Bluetooth initialized.\n"); httpd_init(); sio_print("Applications started.\n"); cb = mem_malloc(sizeof(struct phybusif_cb)); cb->dmabuf = get_dm0ichars(); phybusif_reset(cb); tmr = KS_alloc_timer(); if(tmr == 0) { sio_print("tmr==0!\n"); } KS_start_timer(tmr, (TICKS)0, (TICKS)100/CLKTICK, TIMERSEM); /* TCP timer ticks every 100ms */ /* Reset Bluetooth module */ PD7.0 = 1; /* Enable output */ sio_print("Reseting BT module\n"); P7.0 = 1; /* Stop reset */ KS_delay(SELFTASK,(TICKS)4000/CLKTICK); /* Wait for bluetooth module to init */ /* Control application initialisation */ bt_ip_start(); while(1) { dma_input(cb, dma0bsz); /* Check for input */ /* Handle timers */ if(KS_inqsema(TIMERSEM) == SEMA_DONE) { KS_wait(TIMERSEM); tcp_tmr(); ++bt_timer; if(bt_timer == 10) { l2cap_tmr(); rfcomm_tmr(); ppp_tmr(); bt_timer = 0; ++bt_ip_timer; if(bt_ip_timer == 240) { bt_ip_tmr(); bt_ip_timer = 0; } } } } }
/*-----------------------------------------------------------------------------------*/ int main(int argc, char **argv) { struct phybusif_cb *cb; struct timeval tcptv, bttv, now; struct timezone tz; u8_t btiptmr = 0; #ifdef PERF perf_init("/tmp/minimal.perf"); #endif /* PERF */ #ifdef STATS stats_init(); #endif /* STATS */ mem_init(); memp_init(); pbuf_init(); netif_init(); ip_init(); //udp_init(); tcp_init(); printf("TCP/IP initialized.\n"); lwbt_memp_init(); phybusif_init(); if(hci_init() != ERR_OK) { printf("HCI initialization failed!"); exit(-1); } l2cap_init(); sdp_init(); rfcomm_init(); ppp_init(); printf("Bluetooth initialized.\n"); //echo_init(); httpd_init(); printf("Applications started.\n"); cb = malloc(sizeof(struct phybusif_cb)); phybusif_reset(cb); gettimeofday(&bttv, &tz); /* Initialize Bluetooth timer (1s) */ gettimeofday(&tcptv, &tz); /* Initialize TCP timer (TCP_TMR_INTERVAL) */ /* Host controller initialization for DTs according to LAN access point (LAP) and dial up networking (DUN) profile */ bt_ip_start(NULL); while(1) { phybusif_input(cb); /* Check for input */ gettimeofday(&now, &tz); /* Get current time */ /* Check if TCP timer should be called */ if((now.tv_sec - tcptv.tv_sec) * 1000000 + (now.tv_usec - tcptv.tv_usec) >= TCP_TMR_INTERVAL * 1000) { gettimeofday(&tcptv, &tz); /* Reset TCP timer */ tcp_tmr(); } /* Check if Bluetooth and NAT timers should be called */ if((now.tv_sec - bttv.tv_sec) * 1000000 + (now.tv_usec - bttv.tv_usec) >= 1000000) { gettimeofday(&bttv, &tz); /* Restart Bluetooth timer */ l2cap_tmr(); rfcomm_tmr(); ppp_tmr(); nat_tmr(); if(++btiptmr == 240) { /* Akes server special */ bt_ip_tmr(); btiptmr = 0; } } } return 0; }