1#include "main-header.h" 
    2#include "espressif/esp8266_flash.h" 
    4#include "espressif/esp32_bluetooth.h" 
    7static struct command *bt_get_reply() {
 
   15  dcom->
flags = COMFLAGS_DATA | COMFLAGS_SUCCESS;
 
   18static void bt_peer_update_finished(
const uint8_t *buf, 
int size) {
 
   19  int sent_peers = buf[6];
 
   21  printf(
"[bt-peer-update] final ack for (received %i peers, esp32 sent %i peers)\r\n", peer_info.peercount, sent_peers);
 
   27  for (i = 0; i < MAX_BT_PEERS; i++) {
 
   28    struct bt_peer *peer = &peer_info.peers[i];
 
   33    if (peers_in_com > 5) {
 
   42        printf(
"[bt-peer-update] no command allocatable\r\n");
 
   49    shifter_init(sh, (uint8_t *)&bbuf, 90);
 
   50    shift_into_uint64(sh, peer->mac);
 
   51    shift_into_uint8(sh, strlen(peer->name));
 
   53    for (j = 0; j < strlen(peer->name); j++) {
 
   54      shift_into_uint8(sh, peer->name[j]);
 
   56    if (shift_error(sh)) {
 
   57      printf(
"[bt-peer-update] Shift error: %i\r\n", shift_error(sh));
 
   74  dcom->
flags = COMFLAGS_ACK | COMFLAGS_SUCCESS;
 
   82static void bt_peer_update_continue(
const uint8_t *buf, 
int size) {
 
   83  peer_info.peercount++;
 
   88  for (nc = 0; nc < MAX_BT_PEERS; nc++) {
 
   89    struct bt_peer *p = &peer_info.peers[nc];
 
   96    printf(
"[bt-peer-update] No more peer spaces\r\n");
 
  104  seq |= (((uint32_t)buf[nc++]) << (1 * 24));
 
  105  seq |= (((uint32_t)buf[nc++]) << (1 * 16));
 
  106  seq |= (((uint32_t)buf[nc++]) << (1 * 8));
 
  113  for (i = 0; i < 6; i++) {
 
  114    peer->mac = peer->mac << 8;
 
  115    peer->mac = peer->mac | buf[nc++];
 
  119  if (nl >= 
sizeof(peer->name)) {
 
  120    nl = 
sizeof(peer->name) - 1;
 
  122  for (i = 0; i < nl; i++) {
 
  123    peer->name[i] = buf[nc++];
 
  130void bt_peer_update(
const uint8_t *buf, 
int size) {
 
  132  int follows = buf[5];
 
  134  printf(
"[bt-peer-update] received (idx=%i,follows=%i):\r\n", idx, follows);
 
  135  printHex(
" ", buf, size);
 
  136  if (peer_info.valid == 0) {
 
  137    printf(
"[bt-peer-update] received, but no trigger command\r\n");
 
  142    bt_peer_update_finished(buf, size);
 
  144    bt_peer_update_continue(buf, size);
 
  150int esp32_bt_peers(
struct command *com) {
 
  152  if (peer_info.bt_peer_com == NULL) {
 
  155  peer_info.peercount = 0;
 
  159  for (i = 0; i < MAX_BT_PEERS; i++) {
 
  160    struct bt_peer *peer = &peer_info.peers[i];
 
  163  printf(
"Trigger to get bt peers:\r\n");
 
  167  buf[0] = com->
index && 0xff;
 
  168  buf[1] = (com->
index >> (1 * 8)) && 0xff;
 
  169  buf[2] = (com->
index >> (2 * 8)) && 0xff;
 
  170  buf[3] = (com->
index >> (3 * 8)) && 0xff;
 
  175int esp32_bt_set_characteristic_com(
struct command *com) {
 
  182  const char *argbuf = 
get_arg(com, 0); 
 
  186    printf(
"esp8266: invalid argument size #0 (should be 6, is %i)\r\n", ac);
 
  189  for (i = 0; i < 6; i++) {
 
  190    buf[j++] = argbuf[i];
 
  196    printf(
"esp8266: invalid argument size #1 (should be 2, is %i)\r\n", ac);
 
  199  buf[j++] = argbuf[0];
 
  200  buf[j++] = argbuf[1];
 
  204  int avail = 
sizeof(buf) - 10;
 
  206  if ((ac + j) > avail) {
 
  207    printf(
"esp8266: Invalid argument size #2 (too long %i bytes), available: %i\n", ac, avail - j);
 
  210  for (i = 0; i < ac; i++) {
 
  211    buf[j++] = argbuf[i];
 
  219int esp32_bt_trigger_read_characteristic_com(
struct command *com) {
 
  226  const char *argbuf = 
get_arg(com, 0); 
 
  230    printf(
"esp8266: invalid argument size #0 (should be 6, is %i)\r\n", ac);
 
  233  for (i = 0; i < 6; i++) {
 
  234    buf[j++] = argbuf[i];
 
  240    printf(
"esp8266: invalid argument size #1 (should be 2, is %i)\r\n", ac);
 
  243  buf[j++] = argbuf[0];
 
  244  buf[j++] = argbuf[1];
 
void free_command(struct command *com)
free a command
 
int deliver_command(struct command *com, pkt_callback)
deliver a command to a module
 
void command_add_arg(struct command *com, const char *format,...)
adds an arg to a partially initialised command structure
 
void cat_send_local_instruction(const PACKET_TYPE type, const uint8_t *buf, const int len)
send a packet to the esp8266 wifi chip
 
struct command * get_data_reply(struct command *com)
allocates and initializes a packet to be send as "data" to the command typically you'd add some data ...
 
void command_print(struct command *com)
prints a command in human readable format to serial console
 
int command_add_binary_arg(struct command *com, const int len, const byte *srcbuf)
adds a binary parameter to command returns 0 if ok otherwise errorcode
 
const char * get_arg(const struct command *com, int index)
given an argument by index[0..n], will return a pointer to the bytearray (excluding the fieldtype) th...
 
int get_arg_size(const struct command *com, int index)
given an argument by index [0..n], will returns the size of the value in bytes. for a string/array it...