modubluepy.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2017 Glenn Ruben Bakke
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #ifndef UBLUEPY_H__
  27. #define UBLUEPY_H__
  28. /* Examples:
  29. Advertisment:
  30. from ubluepy import Peripheral
  31. p = Peripheral()
  32. p.advertise(device_name="MicroPython")
  33. DB setup:
  34. from ubluepy import Service, Characteristic, UUID, Peripheral, constants
  35. from board import LED
  36. def event_handler(id, handle, data):
  37. print("BLE event:", id, "handle:", handle)
  38. print(data)
  39. if id == constants.EVT_GAP_CONNECTED:
  40. # connected
  41. LED(2).on()
  42. elif id == constants.EVT_GAP_DISCONNECTED:
  43. # disconnect
  44. LED(2).off()
  45. elif id == 80:
  46. print("id 80, data:", data)
  47. # u0 = UUID("0x180D") # HRM service
  48. # u1 = UUID("0x2A37") # HRM measurement
  49. u0 = UUID("6e400001-b5a3-f393-e0a9-e50e24dcca9e")
  50. u1 = UUID("6e400002-b5a3-f393-e0a9-e50e24dcca9e")
  51. u2 = UUID("6e400003-b5a3-f393-e0a9-e50e24dcca9e")
  52. s = Service(u0)
  53. c0 = Characteristic(u1, props = Characteristic.PROP_WRITE | Characteristic.PROP_WRITE_WO_RESP)
  54. c1 = Characteristic(u2, props = Characteristic.PROP_NOTIFY, attrs = Characteristic.ATTR_CCCD)
  55. s.addCharacteristic(c0)
  56. s.addCharacteristic(c1)
  57. p = Peripheral()
  58. p.addService(s)
  59. p.setConnectionHandler(event_handler)
  60. p.advertise(device_name="micr", services=[s])
  61. */
  62. #include "py/obj.h"
  63. extern const mp_obj_type_t ubluepy_uuid_type;
  64. extern const mp_obj_type_t ubluepy_service_type;
  65. extern const mp_obj_type_t ubluepy_characteristic_type;
  66. extern const mp_obj_type_t ubluepy_peripheral_type;
  67. extern const mp_obj_type_t ubluepy_scanner_type;
  68. extern const mp_obj_type_t ubluepy_scan_entry_type;
  69. extern const mp_obj_type_t ubluepy_constants_type;
  70. extern const mp_obj_type_t ubluepy_constants_ad_types_type;
  71. typedef enum {
  72. UBLUEPY_UUID_16_BIT = 1,
  73. UBLUEPY_UUID_128_BIT
  74. } ubluepy_uuid_type_t;
  75. typedef enum {
  76. UBLUEPY_SERVICE_PRIMARY = 1,
  77. UBLUEPY_SERVICE_SECONDARY = 2
  78. } ubluepy_service_type_t;
  79. typedef enum {
  80. UBLUEPY_ADDR_TYPE_PUBLIC = 0,
  81. UBLUEPY_ADDR_TYPE_RANDOM_STATIC = 1,
  82. #if 0
  83. UBLUEPY_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE = 2,
  84. UBLUEPY_ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE = 3,
  85. #endif
  86. } ubluepy_addr_type_t;
  87. typedef enum {
  88. UBLUEPY_ROLE_PERIPHERAL,
  89. UBLUEPY_ROLE_CENTRAL
  90. } ubluepy_role_type_t;
  91. typedef struct _ubluepy_uuid_obj_t {
  92. mp_obj_base_t base;
  93. ubluepy_uuid_type_t type;
  94. uint8_t value[2];
  95. uint8_t uuid_vs_idx;
  96. } ubluepy_uuid_obj_t;
  97. typedef struct _ubluepy_peripheral_obj_t {
  98. mp_obj_base_t base;
  99. ubluepy_role_type_t role;
  100. volatile uint16_t conn_handle;
  101. mp_obj_t delegate;
  102. mp_obj_t notif_handler;
  103. mp_obj_t conn_handler;
  104. mp_obj_t service_list;
  105. } ubluepy_peripheral_obj_t;
  106. typedef struct _ubluepy_service_obj_t {
  107. mp_obj_base_t base;
  108. uint16_t handle;
  109. uint8_t type;
  110. ubluepy_uuid_obj_t * p_uuid;
  111. ubluepy_peripheral_obj_t * p_periph;
  112. mp_obj_t char_list;
  113. uint16_t start_handle;
  114. uint16_t end_handle;
  115. } ubluepy_service_obj_t;
  116. typedef struct _ubluepy_characteristic_obj_t {
  117. mp_obj_base_t base;
  118. uint16_t handle;
  119. ubluepy_uuid_obj_t * p_uuid;
  120. uint16_t service_handle;
  121. uint16_t user_desc_handle;
  122. uint16_t cccd_handle;
  123. uint16_t sccd_handle;
  124. uint8_t props;
  125. uint8_t attrs;
  126. ubluepy_service_obj_t * p_service;
  127. mp_obj_t value_data;
  128. } ubluepy_characteristic_obj_t;
  129. typedef struct _ubluepy_descriptor_obj_t {
  130. mp_obj_base_t base;
  131. uint16_t handle;
  132. ubluepy_uuid_obj_t * p_uuid;
  133. } ubluepy_descriptor_obj_t;
  134. typedef struct _ubluepy_delegate_obj_t {
  135. mp_obj_base_t base;
  136. } ubluepy_delegate_obj_t;
  137. typedef struct _ubluepy_advertise_data_t {
  138. uint8_t * p_device_name;
  139. uint8_t device_name_len;
  140. mp_obj_t * p_services;
  141. uint8_t num_of_services;
  142. uint8_t * p_data;
  143. uint8_t data_len;
  144. bool connectable;
  145. } ubluepy_advertise_data_t;
  146. typedef struct _ubluepy_scanner_obj_t {
  147. mp_obj_base_t base;
  148. mp_obj_t adv_reports;
  149. } ubluepy_scanner_obj_t;
  150. typedef struct _ubluepy_scan_entry_obj_t {
  151. mp_obj_base_t base;
  152. mp_obj_t addr;
  153. uint8_t addr_type;
  154. bool connectable;
  155. int8_t rssi;
  156. mp_obj_t data;
  157. } ubluepy_scan_entry_obj_t;
  158. typedef enum _ubluepy_prop_t {
  159. UBLUEPY_PROP_BROADCAST = 0x01,
  160. UBLUEPY_PROP_READ = 0x02,
  161. UBLUEPY_PROP_WRITE_WO_RESP = 0x04,
  162. UBLUEPY_PROP_WRITE = 0x08,
  163. UBLUEPY_PROP_NOTIFY = 0x10,
  164. UBLUEPY_PROP_INDICATE = 0x20,
  165. UBLUEPY_PROP_AUTH_SIGNED_WR = 0x40,
  166. } ubluepy_prop_t;
  167. typedef enum _ubluepy_attr_t {
  168. UBLUEPY_ATTR_CCCD = 0x01,
  169. UBLUEPY_ATTR_SCCD = 0x02,
  170. } ubluepy_attr_t;
  171. #endif // UBLUEPY_H__