• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Chipsea Technologies (Shenzhen) Corp., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ble_ip_config.h"
17 
18 #if (BLE_SMARTCONFIG)
19 #include "hal_gap.h"
20 #include "hal_gattc_task.h"
21 #include "hal_attm.h"
22 #include "hal_gapc_task.h"
23 #include "smartconfig.h"
24 #include "smartconfig_task.h"
25 #include "hal_profile_utils.h"
26 #include "ble_ke_mem.h"
27 #include "ble_common_utils.h"
28 
29 
30 #define smartconfig_service_uuid_128_content                {0xfb,0x34,0x9b,0x5f,0x80,0x00,0x00,0x80,0x00,0x10,0x00,0x00,0xe7,0xfe,0x00,0x00}
31 #define smartconfig_cmd_ap_info_val_uuid_128_content        {0xfb,0x34,0x9b,0x5f,0x80,0x00,0x00,0x80,0x00,0x10,0x00,0x00,0xc7,0xfe,0x00,0x00}
32 #define smartconfig_cmd_wifi_state_val_uuid_128_content     {0xfb,0x34,0x9b,0x5f,0x80,0x00,0x00,0x80,0x00,0x10,0x00,0x00,0xc8,0xfe,0x00,0x00}
33 #define smartconfig_time_state_char_val_uuid_128_content    {0xfb,0x34,0x9b,0x5f,0x80,0x00,0x00,0x80,0x00,0x10,0x00,0x00,0xc9,0xfe,0x00,0x00}
34 #define smartconfig_time_char_val_uuid_128_content          {0xfb,0x34,0x9b,0x5f,0x80,0x00,0x00,0x80,0x00,0x10,0x00,0x00,0xca,0xfe,0x00,0x00}
35 
36 #define ATT_DECL_PRIMARY_SERVICE_UUID           {0x00, 0x28}
37 #define ATT_DECL_CHARACTERISTIC_UUID            {0x03, 0x28}
38 #define ATT_DESC_CLIENT_CHAR_CFG_UUID           {0x02, 0x29}
39 
40 static const uint8_t SMARTCONFIG_SERVICE_UUID_128[ATT_UUID_128_LEN]    = smartconfig_service_uuid_128_content;
41 
42 /// Full SMARTCONFIG SERVER Database Description - Used to add attributes into the database
43 const struct attm_desc_128 smartconfig_att_db[SMARTCONFIG_IDX_NB] =
44 {
45     // Service Declaration
46     [SMARTCONFIG_IDX_SVC]                =   {ATT_DECL_PRIMARY_SERVICE_UUID, PERM(RD, ENABLE), 0, 0},
47 
48     // Command TX Characteristic Declaration
49     [SMARTCONFIG_IDX_WIFI_STATE_CHAR]        =   {ATT_DECL_CHARACTERISTIC_UUID, PERM(RD, ENABLE), 0, 0},
50     // Command TX Characteristic Value
51     [SMARTCONFIG_IDX_WIFI_STATE_VAL]         =   {smartconfig_cmd_wifi_state_val_uuid_128_content, PERM(NTF, ENABLE) | PERM(IND, ENABLE) | PERM(RD, ENABLE),
52         PERM(RI, ENABLE) | PERM_VAL(UUID_LEN, PERM_UUID_128), SMARTCONFIG_MAX_LEN},
53     // Command TX Characteristic - Client Characteristic Configuration Descriptor
54     [SMARTCONFIG_IDX_WIFI_STATE_NTF_CFG]     =   {ATT_DESC_CLIENT_CHAR_CFG_UUID, PERM(RD, ENABLE) | PERM(WRITE_REQ, ENABLE), 0, 0},
55 
56     // Command RX Characteristic Declaration
57     [SMARTCONFIG_IDX_AP_INFO_CHAR]        =   {ATT_DECL_CHARACTERISTIC_UUID, PERM(RD, ENABLE), 0, 0},
58     // Command RX Characteristic Value
59     [SMARTCONFIG_IDX_AP_INFO_VAL]         =   {smartconfig_cmd_ap_info_val_uuid_128_content,
60         PERM(WRITE_REQ, ENABLE),
61         PERM(RI, ENABLE) | PERM_VAL(UUID_LEN, PERM_UUID_128), SMARTCONFIG_MAX_LEN},
62 
63     // Data TX Characteristic Declaration
64     [SMARTCONFIG_IDX_TIME_STATE_CHAR]       =   {ATT_DECL_CHARACTERISTIC_UUID, PERM(RD, ENABLE), 0, 0},
65     // Data TX Characteristic Value
66     [SMARTCONFIG_IDX_TIME_STATE_VAL]        =   {smartconfig_time_state_char_val_uuid_128_content, PERM(NTF, ENABLE) | PERM(IND, ENABLE) | PERM(RD, ENABLE),
67         PERM(RI, ENABLE) | PERM_VAL(UUID_LEN, PERM_UUID_128), SMARTCONFIG_MAX_LEN},
68     // Data TX Characteristic - Client Characteristic Configuration Descriptor
69     [SMARTCONFIG_IDX_TIME_STATE_NTF_CFG]    =   {ATT_DESC_CLIENT_CHAR_CFG_UUID, PERM(RD, ENABLE) | PERM(WRITE_REQ, ENABLE), 0, 0},
70 
71     // Data RX Characteristic Declaration
72     [SMARTCONFIG_IDX_TIME_INFO_CHAR]       =   {ATT_DECL_CHARACTERISTIC_UUID, PERM(RD, ENABLE), 0, 0},
73     // Data RX Characteristic Value
74     [SMARTCONFIG_IDX_TIME_INFO_VAL]        =   {smartconfig_time_char_val_uuid_128_content,
75         PERM(WRITE_REQ, ENABLE) | PERM(WRITE_COMMAND, ENABLE),
76         PERM(RI, ENABLE) | PERM_VAL(UUID_LEN, PERM_UUID_128), SMARTCONFIG_MAX_LEN},
77 
78 };
79 
80 /**
81  ****************************************************************************************
82  * @brief Initialization of the SMARTCONFIG module.
83  * This function performs all the initializations of the Profile module.
84  *  - Creation of database (if it's a service)
85  *  - Allocation of profile required memory
86  *  - Initialization of task descriptor to register application
87  *      - Task State array
88  *      - Number of tasks
89  *      - Default task handler
90  *
91  * @param[out]    env        Collector or Service allocated environment data.
92  * @param[in|out] start_hdl  Service start handle (0 - dynamically allocated), only applies for services.
93  * @param[in]     app_task   Application task number.
94  * @param[in]     sec_lvl    Security level (AUTH, EKS and MI field of @see enum attm_value_perm_mask)
95  * @param[in]     param      Configuration parameters of profile collector or service (32 bits aligned)
96  *
97  * @return status code to know if profile initialization succeed or not.
98  ****************************************************************************************
99  */
smartconfig_init(struct prf_task_env * env,uint16_t * start_hdl,uint16_t app_task,uint8_t sec_lvl,void * params)100 static uint8_t smartconfig_init(struct prf_task_env* env, uint16_t* start_hdl,
101     uint16_t app_task, uint8_t sec_lvl, void* params)
102 {
103     dbg("%s \r\n", __func__);
104 
105     uint8_t status;
106 
107     //Add Service Into Database
108     status = attm_svc_create_db_128(start_hdl, SMARTCONFIG_SERVICE_UUID_128, NULL,
109             SMARTCONFIG_IDX_NB, NULL, env->task, &smartconfig_att_db[0],
110             (sec_lvl & (PERM_MASK_SVC_DIS | PERM_MASK_SVC_AUTH | PERM_MASK_SVC_EKS))| PERM(SVC_MI, DISABLE)
111             | PERM_VAL(SVC_UUID_LEN, PERM_UUID_128));
112 
113     //-------------------- allocate memory required for the profile  ---------------------
114     if (status == ATT_ERR_NO_ERROR)
115     {
116         // Allocate SMARTCONFIG required environment variable
117         struct smartconfig_env_tag* smartconfig_env =
118                 (struct smartconfig_env_tag* ) ke_malloc(sizeof(struct smartconfig_env_tag), KE_MEM_ATT_DB);
119 
120         // Initialize SMARTCONFIG environment
121         env->env = (prf_env_t*) smartconfig_env;
122         smartconfig_env->shdl = *start_hdl;
123 
124         smartconfig_env->prf_env.app_task = app_task
125                 | (PERM_GET(sec_lvl, SVC_MI) ? PERM(PRF_MI, ENABLE) : PERM(PRF_MI, DISABLE));
126         // Mono Instantiated task
127         smartconfig_env->prf_env.prf_task = env->task | PERM(PRF_MI, DISABLE);
128 
129         // initialize environment variable
130         env->id = TASK_ID_SMARTCONFIG;
131         smartconfig_task_init(&(env->desc));
132 
133         /* Put HRS in Idle state */
134         ke_state_set(env->task, SMARTCONFIG_IDLE);
135     }
136 
137     return (status);
138 }
139 
140 /**
141  ****************************************************************************************
142  * @brief Destruction of the SMARTCONFIG module - due to a reset for instance.
143  * This function clean-up allocated memory (attribute database is destroyed by another
144  * procedure)
145  *
146  * @param[in|out]    env        Collector or Service allocated environment data.
147  ****************************************************************************************
148  */
smartconfig_destroy(struct prf_task_env * env)149 static void smartconfig_destroy(struct prf_task_env* env)
150 {
151     struct smartconfig_env_tag* smartconfig_env = (struct smartconfig_env_tag*) env->env;
152 
153     // free profile environment variables
154     env->env = NULL;
155     ke_free(smartconfig_env);
156 }
157 
158 /**
159  ****************************************************************************************
160  * @brief Handles Connection creation
161  *
162  * @param[in|out]    env        Collector or Service allocated environment data.
163  * @param[in]        conidx     Connection index
164  ****************************************************************************************
165  */
smartconfig_create(struct prf_task_env * env,uint8_t conidx)166 static void smartconfig_create(struct prf_task_env* env, uint8_t conidx)
167 {
168     struct smartconfig_env_tag* smartconfig_env = (struct smartconfig_env_tag*) env->env;
169     struct prf_svc smartconfig_smartconfigc = {smartconfig_env->shdl, smartconfig_env->shdl + SMARTCONFIG_IDX_NB};
170     prf_register_atthdl2gatt(env->env, conidx, &smartconfig_smartconfigc);
171 }
172 
173 /**
174  ****************************************************************************************
175  * @brief Handles Disconnection
176  *
177  * @param[in|out]    env        Collector or Service allocated environment data.
178  * @param[in]        conidx     Connection index
179  * @param[in]        reason     Detach reason
180  ****************************************************************************************
181  */
smartconfig_cleanup(struct prf_task_env * env,uint8_t conidx,uint8_t reason)182 static void smartconfig_cleanup(struct prf_task_env* env, uint8_t conidx, uint8_t reason)
183 {
184     /* Nothing to do */
185 }
186 
187 /*
188  * GLOBAL VARIABLE DEFINITIONS
189  ****************************************************************************************
190  */
191 
192 /// SMARTCONFIG Task interface required by profile manager
193 const struct prf_task_cbs smartconfig_itf =
194 {
195     (prf_init_fnct) smartconfig_init,
196     smartconfig_destroy,
197     smartconfig_create,
198     smartconfig_cleanup,
199 };
200 
201 /*
202  * EXPORTED FUNCTIONS DEFINITIONS
203  ****************************************************************************************
204  */
205 
smartconfig_prf_itf_get(void)206 const struct prf_task_cbs* smartconfig_prf_itf_get(void)
207 {
208    return &smartconfig_itf;
209 }
210 
211 
212 #endif /* BLE_SMARTCONFIG */
213