1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 
5 #ifndef __GENIE_AIS_H__
6 #define __GENIE_AIS_H__
7 
8 #include <misc/byteorder.h>
9 #include <bluetooth/gatt.h>
10 
11 #define AIS_DISCONNECT_TIMEOUT 1000 //1s
12 
13 #define AIS_SERVICE_UUID BT_UUID_DECLARE_16(0xFEB3)
14 #define AIS_READ_UUID BT_UUID_DECLARE_16(0xFED4)
15 #define AIS_WRITE_UUID BT_UUID_DECLARE_16(0xFED5)
16 #define AIS_INDICATE_UUID BT_UUID_DECLARE_16(0xFED6)
17 #define AIS_WRITE_WO_RESP_UUID BT_UUID_DECLARE_16(0xFED7)
18 #define AIS_NOTIFY_UUID BT_UUID_DECLARE_16(0xFED8)
19 
20 enum
21 {
22     AIS_STATE_DISCON,
23     AIS_STATE_CONNECT,
24     AIS_STATE_AUTH,
25     AIS_STATE_IDLE,
26     AIS_STATE_OTA,
27     AIS_STATE_REBOOT,
28 };
29 
30 enum
31 {
32     AIS_RESP_ERR = 0x0F,
33     AIS_SCRT_RANDOM = 0x10,
34     AIS_SCRT_CIPHER = 0x11,
35     AIS_SCRT_RESULT = 0x12,
36     AIS_SCRT_ACK = 0x13,
37     AIS_LINK_STATUS = 0x14,
38     AIS_LINK_ACK = 0x15,
39 };
40 
41 //AIS PDU FORMAT
42 typedef struct
43 {
44     uint8_t msg_id : 4;
45     uint8_t enc : 1;
46     uint8_t ver : 3;
47     uint8_t cmd;
48     uint8_t seq : 4;
49     uint8_t total_frame : 4;
50     uint8_t payload_len;
51 } __attribute__((packed)) ais_header_t;
52 
53 typedef struct
54 {
55     uint8_t random[16];
56 } __attribute__((packed)) ais_scrt_random_t;
57 
58 typedef struct
59 {
60     uint8_t cipher[16];
61 } __attribute__((packed)) ais_scrt_cipher_t;
62 
63 typedef struct
64 {
65     uint8_t result;
66 } __attribute__((packed)) ais_scrt_result_t;
67 
68 typedef struct
69 {
70     uint8_t ack;
71 } __attribute__((packed)) ais_scrt_ack_t;
72 
73 typedef struct
74 {
75     ais_header_t header;
76     uint8_t payload[16];
77 } __attribute__((packed)) ais_pdu_t;
78 
79 typedef struct _genie_ais_ctx_s
80 {
81     uint8_t state;
82     struct bt_conn *p_conn;
83     k_timer_t state_update_timer;
84     k_timer_t disconnect_timer;
85 } genie_ais_ctx_t;
86 
87 /**
88  * @brief the ais service action when gatt is connected.
89  * @param[in] the handle of connect.
90  */
91 void genie_ais_connect(struct bt_conn *p_conn);
92 
93 /**
94  * @brief the ais service action when gatt is disconnected.
95  * @param[in] the disconnect reason.
96  */
97 void genie_ais_disconnect(uint8_t reason);
98 
99 /**
100  * @brief regist the ais service.
101  * @return the result of registion.
102  */
103 int genie_ais_init(void);
104 
105 int genie_ais_pre_init(void);
106 
107 int genie_ais_state_set(uint8_t state);
108 
109 uint8_t genie_ais_state_get(void);
110 
111 void genie_ais_notify(uint8_t msg_id, uint8_t cmd, uint8_t *p_msg, uint16_t len);
112 
113 #endif
114