/* ============================================================================
* IWRAP RESPONSE AND EVENT HANDLER IMPLEMENTATIONS
* ========================================================================= */
void my_iwrap_rsp_call(uint8_t link_id) {
iwrap_pending_calls++;
iwrap_pending_call_link_id = link_id;
iwrap_autocall_index = (iwrap_autocall_index + 1) % iwrap_pairings;
iwrap_state = IWRAP_STATE_PENDING_CALL;
}
void my_iwrap_rsp_list_count(uint8_t num_of_connections) {
iwrap_active_connections = num_of_connections;
}
void my_iwrap_rsp_list_result(uint8_t link_id, const char *mode, uint16_t blocksize, uint32_t elapsed_time, uint16_t local_msc, uint16_t remote_msc, const iwrap_address_t *addr, uint16_t channel, uint8_t direction, uint8_t powermode, uint8_t role, uint8_t crypt, uint16_t buffer, uint8_t eretx) {
add_mapped_connection(link_id, addr, mode, channel);
}
void my_iwrap_rsp_set(uint8_t category, const char *option, const char *value) {
if (category == IWRAP_SET_CATEGORY_BT) {
if (strncmp((char *)option, "BDADDR", 6) == 0) {
iwrap_address_t local_mac;
iwrap_hexstrtobin((char *)value, 0, local_mac.address, 0);
char *local_mac_str = (char *)malloc(18);
if (local_mac_str) {
iwrap_bintohexstr(local_mac.address, 6, &local_mac_str, ':', 1);
serial_out(F(":: Module MAC is "));
serial_out(local_mac_str);
serial_out(F("\n"));
free(local_mac_str);
}
} else if (strncmp((char *)option, "NAME", 4) == 0) {
serial_out(F(":: Friendly name is "));
serial_out(value);
serial_out(F("\n"));
} else if (strncmp((char *)option, "PAIR", 4) == 0) {
iwrap_address_t remote_mac;
iwrap_hexstrtobin((char *)value, 0, remote_mac.address, 0);
// make sure we allocate memory for the connection map entry
if (iwrap_connection_map[iwrap_pairings] == 0) {
iwrap_connection_map[iwrap_pairings] = (iwrap_connection_t *)malloc(sizeof(iwrap_connection_t));
}
memset(iwrap_connection_map[iwrap_pairings], 0xFF, sizeof(iwrap_connection_t)); // 0xFF is "no link ID"
memcpy(&(iwrap_connection_map[iwrap_pairings] -> mac), &remote_mac, sizeof(iwrap_address_t));
iwrap_connection_map[iwrap_pairings] -> active_links = 0;
iwrap_pairings++;
char *remote_mac_str = (char *)malloc(18);
if (remote_mac_str) {
iwrap_bintohexstr(remote_mac.address, 6, &remote_mac_str, ':', 1);
serial_out(F(":: Pairing (MAC="));
serial_out(remote_mac_str);
serial_out(F(", key="));
serial_out(value + 18);
serial_out(F(")\n"));
free(remote_mac_str);
}
}
}
}
void my_iwrap_evt_connect(uint8_t link_id, const char *type, uint16_t target, const iwrap_address_t *address) {
if (iwrap_pending_call_link_id == link_id) {
if (iwrap_pending_calls) iwrap_pending_calls--;
if (iwrap_state == IWRAP_STATE_PENDING_CALL) iwrap_state = IWRAP_STATE_IDLE;
iwrap_pending_call_link_id = 0xFF;
}
iwrap_active_connections++;
add_mapped_connection(link_id, address, type, target);
print_connection_map();
}
void my_iwrap_evt_no_carrier(uint8_t link_id, uint16_t error_code, const char *message) {
if (iwrap_pending_call_link_id == link_id) {
if (iwrap_pending_calls) iwrap_pending_calls--;
if (iwrap_state == IWRAP_STATE_PENDING_CALL) iwrap_state = IWRAP_STATE_IDLE;
iwrap_pending_call_link_id = 0xFF;
}
if (remove_mapped_connection(link_id) != 0xFF) {
// only update and reprint if the connection was already mapped
// (i.e. not already closed or a failed outgoing connection attempt)
if (iwrap_active_connections) iwrap_active_connections--;
print_connection_map();
}
}
void my_iwrap_evt_pair(const iwrap_address_t *address, uint8_t key_type, const uint8_t *link_key) {
// request pair list again (could be a new pair, or updated pair, or new + overwritten pair)
iwrap_send_command("SET BT PAIR", iwrap_mode);
iwrap_state = IWRAP_STATE_PENDING_SET;
}
void my_iwrap_evt_ready() {
iwrap_state = IWRAP_STATE_UNKNOWN;
}
void my_iwrap_evt_ring(uint8_t link_id, const iwrap_address_t *address, uint8_t channel, const char *profile) {
add_mapped_connection(link_id, address, profile, channel);
print_connection_map();
}