void pan1322::Step() { while (!this->usart->isRxBufferEmpty()) { // uint8_t c = this->usart->Peek(); // DEBUG("1322>%02X %c (%d)\n", c, c, this->usart->rx_len); this->Parse(this->usart->Read()); } if (this->p_cmd != pan_cmd_none) { DEBUG("cmd %d\n", this->p_cmd); switch(this->p_cmd) { case(pan_cmd_set_name): this->SetName(PSTR("SkyDrop Pan1322")); break; case(pan_cmd_create_service): this->CreateService(PSTR("0000110100001000800000805f9b34fb"), PSTR("SPP"), 01, PSTR("080510")); break; case(pan_cmd_set_discoverable): this->SetDiscoverable(true); break; case(pan_cmd_accept_connection): this->AcceptConnection(); break; } this->p_last_cmd = this->p_cmd; this->p_cmd = pan_cmd_none; } if (this->timer != BT_NO_TIMEOUT && this->timer < task_get_ms_tick()) { DEBUG("PAN1322 timeout, last cmd %d\n", this->p_last_cmd); this->timer = BT_NO_TIMEOUT; switch (this->p_last_cmd) { case(pan_cmd_reset): //ROK not recieved bt_irgh(BT_IRQ_INIT_FAIL, 0); break; case(pan_cmd_set_name): this->SetName(PSTR("SkyDrop Pan1322")); break; case(pan_cmd_accept_connection): this->SetName(PSTR("SkyDrop Pan1322")); break; } } }
void bt_module_deinit() { GpioWrite(BT_EN, LOW); GpioWrite(BT_RESET, LOW); bt_irgh(BT_IRQ_DEINIT, 0); bt_uart.Stop(); BT_UART_PWR_OFF; }
void pan1322::Init(Usart * uart) { this->usart = uart; bt_irgh(BT_IRQ_INIT, 0); this->Restart(); this->connected = false; }
void pan1322::Parse(uint8_t c) { uint8_t param; uint8_t tmp; uint8_t n; // DEBUG("P %c %d,%d\n", c, this->p_state, this->p_len); uint8_t state = this->p_state; switch (state) { case(BT_STATE_START): if (c == '+') { this->p_state = BT_STATE_RESP; this->p_len = 4; this->p_index = 0; break; } if (c == 'E') { this->p_state = BT_STATE_ERR; this->p_len = 4; this->p_index = 0; break; } if (c == 'O') { this->p_state = BT_STATE_OK; break; } if (c == 'R') { this->p_state = BT_STATE_ROK; this->p_len = 2; this->p_index = 0; break; } break; case(BT_STATE_ROK): if (this->p_len > this->p_index) { this->p_buff[this->p_index] = c; this->p_index++; } else { if (cmpn(this->p_buff, "OK", 2)) { DEBUG("pan1322 reset ok\n"); this->p_cmd = pan_cmd_set_name; this->p_state = BT_STATE_FIND_RN; } } break; case(BT_STATE_OK): if (c == 'K') { this->timer = BT_NO_TIMEOUT; // DEBUG("OK %d\n", this->p_last_cmd); switch(this->p_last_cmd) { case(pan_cmd_set_name): this->p_cmd = pan_cmd_create_service; break; case(pan_cmd_create_service): this->p_cmd = pan_cmd_set_discoverable; break; case(pan_cmd_set_discoverable): DEBUG("pan1322 init ok\n"); bt_irgh(BT_IRQ_INIT_OK, 0); break; } } this->p_state = BT_STATE_FIND_RN; break; case(BT_STATE_FIND_RN): if (c == '\n') { this->p_state = BT_STATE_START; } break; case(BT_STATE_ERR): if (this->p_len > this->p_index) { this->p_buff[this->p_index] = c; this->p_index++; } else { if (cmpn(this->p_buff, "RR=-", 4)) { param = c - '0'; this->error = true; this->p_state = BT_STATE_FIND_RN; bt_irgh(BT_IRQ_ERROR, ¶m); } } break; case(BT_STATE_RESP): if (this->p_len > this->p_index) { this->p_buff[this->p_index] = c; this->p_index++; } else { if (cmpn(this->p_buff, "RSLE", 4)) { //pair sucesfull this->p_state = BT_STATE_FIND_RN; bt_irgh(BT_IRQ_PAIR, 0); break; } if (cmpn(this->p_buff, "RCOI", 4)) { //TODO: decode btaddress this->p_state = BT_STATE_FIND_RN; // this->AcceptConnection(); this->p_cmd = pan_cmd_accept_connection; break; } if (cmpn(this->p_buff, "RCCR", 4)) { //device connected this->p_state = BT_STATE_FIND_RN; this->connected = true; bt_irgh(BT_IRQ_CONNECTED, 0); break; } if (cmpn(this->p_buff, "RDII", 4)) { //device disconnected this->p_state = BT_STATE_FIND_RN; this->connected = false; bt_irgh(BT_IRQ_DISCONNECTED, 0); break; } if (cmpn(this->p_buff, "RDAI", 4)) { //incoming data this->p_state = BT_STATE_DATA; this->p_len = 0; break; } this->p_state = BT_STATE_FIND_RN; } break; case(BT_STATE_DATA): tmp = this->p_len; switch(tmp) { case(0): // first digit this->p_len++; n = c - '0'; assert(n <= 9); if (n > 9) { this->p_state = BT_STATE_START; break; } this->data_len += 100 * n; break; case(1): // second digit this->p_len++; n = c - '0'; assert(n <= 9); if (n > 9) { this->p_state = BT_STATE_START; break; } this->data_len += 10 * n; break; case(2): // third digit this->p_len++; n = c - '0'; assert(n <= 9); if (n > 9) { this->p_state = BT_STATE_START; break; } this->data_len += 1 * n; break; case(3): // semicolon this->p_len++; assert(c == ','); if (c != ',') { this->p_state = BT_STATE_START; break; } break; case(4): // data this->data_len -= 1; if (this->data_len == 0) { this->p_state = BT_STATE_FIND_RN; } //HANDLE INCOMING DATA HERE //...drop data here :)... break; } break; } }