Gleb Klochkov / Mbed OS Climatcontroll_Main

Dependencies:   esp8266-driver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mac_response_handler.c Source File

mac_response_handler.c

00001 /*
00002  * Copyright (c) 2016-2017, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "nsconfig.h"
00018 #include <string.h>
00019 #include "mlme.h"
00020 #include "common_functions.h"
00021 #include "ns_trace.h"
00022 #include "NWK_INTERFACE/Include/protocol_abstract.h"
00023 #include "NWK_INTERFACE/Include/protocol_timer.h"
00024 #include "Service_Libs/mle_service/mle_service_api.h"
00025 #include "Core/include/address.h"
00026 #include "Core/include/socket.h"
00027 #include "6LoWPAN/Thread/thread_common.h"
00028 #include "6LoWPAN/Thread/thread_management_internal.h"
00029 #include "6LoWPAN/MAC/mac_helper.h"
00030 #include "MLE/mle.h"
00031 #include "mac_mcps.h"
00032 #include "6LoWPAN/MAC/mac_data_poll.h"
00033 #include "6LoWPAN/MAC/mac_response_handler.h"
00034 #include "6LoWPAN/lowpan_adaptation_interface.h"
00035 
00036 static bool mac_data_is_broadcast_addr(const sockaddr_t *addr);
00037 
00038 #define TRACE_GROUP "MRsH"
00039 
00040 
00041 static void mac_mlme_device_table_confirmation_handle(protocol_interface_info_entry_t *info_entry, mlme_get_conf_t *confirmation) {
00042     if (confirmation->value_size != sizeof(mlme_device_descriptor_t)) {
00043         return;
00044     }
00045 
00046     mlme_device_descriptor_t *descpription = (mlme_device_descriptor_t*)confirmation->value_pointer;
00047 
00048     tr_debug("Dev stable get confirmation %x", confirmation->status);
00049 
00050     if (confirmation->status == MLME_SUCCESS) {
00051         //GET ME table by extended mac64 address
00052         mle_neigh_table_entry_t * entry = mle_class_get_by_link_address(info_entry->id, descpription->ExtAddress, ADDR_802_15_4_LONG );
00053         if (!entry) {
00054             return;
00055         }
00056 
00057         if (entry->short_adr != descpription->ShortAddress) {
00058             //Refresh Short ADDRESS
00059             mlme_set_t set_request;
00060             descpription->ShortAddress = entry->short_adr;
00061 
00062             //CALL MLME-SET
00063             set_request.attr = macDeviceTable;
00064             set_request.attr_index = confirmation->attr_index;
00065             set_request.value_pointer = descpription;
00066             set_request.value_size = confirmation->value_size;
00067             info_entry->mac_api->mlme_req(info_entry->mac_api, MLME_SET, &set_request);
00068         }
00069 
00070     }
00071 }
00072 
00073 static void mac_mlme_frame_counter_confirmation_handle(protocol_interface_info_entry_t *info_entry, mlme_get_conf_t *confirmation) {
00074     if (confirmation->value_size != 4) {
00075         return;
00076     }
00077     uint32_t *temp_ptr = (uint32_t*)confirmation->value_pointer;
00078     info_entry->mac_parameters->security_frame_counter = *temp_ptr;
00079 }
00080 
00081 static void mac_mlme_get_confirmation_handler(protocol_interface_info_entry_t *info_entry, mlme_get_conf_t *confirmation) {
00082 
00083     if (!confirmation) {
00084         return;
00085     }
00086     switch (confirmation->attr) {
00087         case macDeviceTable:
00088             mac_mlme_device_table_confirmation_handle(info_entry, confirmation);
00089             break;
00090 
00091         case macFrameCounter:
00092             mac_mlme_frame_counter_confirmation_handle(info_entry, confirmation);
00093             break;
00094 
00095         default:
00096 
00097             break;
00098 
00099     }
00100 }
00101 
00102 void mcps_data_confirm_handler( const mac_api_t* api, const mcps_data_conf_t *data )
00103 {
00104     protocol_interface_info_entry_t *info_entry = protocol_stack_interface_info_get_by_id(api->parent_id);
00105     //TODO: create buffer_t and call correct function
00106     //Update protocol_status
00107     lowpan_adaptation_interface_tx_confirm(info_entry, data);
00108 }
00109 
00110 static bool mcps_data_indication_neighbor_validate(int8_t interface_id, const sockaddr_t *addr)
00111 {
00112     /* If MLE is enabled, we will talk if we have an MLE association */
00113     mle_neigh_table_entry_t *mle_entry = mle_class_get_by_link_address(interface_id, addr->address  + 2, addr->addr_type );
00114     if (mle_entry && (mle_entry->handshakeReady ||  mle_entry->thread_commission)) {
00115         return true;
00116     }
00117 
00118     /* Otherwise, we don't know them */
00119     return false;
00120 
00121 }
00122 
00123 void mcps_data_indication_handler( const mac_api_t* api, const mcps_data_ind_t *data_ind )
00124 {
00125     protocol_interface_info_entry_t *info_entry = protocol_stack_interface_info_get_by_id(api->parent_id);
00126     buffer_t *buf = buffer_get(data_ind->msduLength);
00127     if (!buf || !info_entry) {
00128         return;
00129     }
00130     uint8_t *ptr;
00131     buffer_data_add(buf, data_ind->msdu_ptr, data_ind->msduLength);
00132     //tr_debug("MAC Paylod size %u %s",data_ind->msduLength, trace_array(data_ind->msdu_ptr, 8));
00133     buf->options .lqi  = data_ind->mpduLinkQuality;
00134     buf->options .dbm  = data_ind->signal_dbm;
00135     buf->src_sa .addr_type  = (addrtype_t)data_ind->SrcAddrMode;
00136     ptr = common_write_16_bit(data_ind->SrcPANId, buf->src_sa .address );
00137     memcpy(ptr, data_ind->SrcAddr, 8);
00138     buf->dst_sa .addr_type  = (addrtype_t)data_ind->DstAddrMode;
00139     ptr = common_write_16_bit(data_ind->DstPANId, buf->dst_sa .address );
00140     memcpy(ptr, data_ind->DstAddr, 8);
00141     //Set Link spesific stuff to seperately
00142     buf->link_specific.ieee802_15_4.srcPanId = data_ind->SrcPANId;
00143     buf->link_specific.ieee802_15_4.dstPanId = data_ind->DstPANId;
00144 
00145     if (mac_data_is_broadcast_addr(&buf->dst_sa )) {
00146         buf->options .ll_broadcast_rx  = true;
00147     }
00148     buf->interface  = info_entry;
00149     if (data_ind->Key.SecurityLevel) {
00150         buf->link_specific.ieee802_15_4.fc_security = true;
00151 
00152         if (info_entry->mac_security_key_usage_update_cb) {
00153             info_entry->mac_security_key_usage_update_cb(info_entry, &data_ind->Key);
00154         }
00155     } else {
00156         buf->link_specific.ieee802_15_4.fc_security = false;
00157         if (mac_helper_default_security_level_get(info_entry) ||
00158                 !mcps_data_indication_neighbor_validate(info_entry->id, &buf->src_sa )) {
00159             //SET By Pass
00160             buf->options .ll_security_bypass_rx  = true;
00161         }
00162     }
00163 
00164     buf->info  = (buffer_info_t)(B_TO_IPV6_TXRX | B_FROM_MAC | B_DIR_UP);
00165     protocol_push(buf);
00166 }
00167 
00168 void mcps_purge_confirm_handler( const mac_api_t* api, mcps_purge_conf_t *data )
00169 {
00170     (void)api;
00171     tr_info("MCPS Data Purge confirm status %u, for handle %u", data->status, data->msduHandle);
00172 }
00173 
00174 static void stop_bootstrap_timer(protocol_interface_info_entry_t *info_entry)
00175 {
00176     if (info_entry->bootsrap_mode == ARM_NWK_BOOTSRAP_MODE_6LoWPAN_BORDER_ROUTER) {
00177         protocol_timer_stop(PROTOCOL_TIMER_BOOTSTRAP_TIM);
00178     }
00179 }
00180 
00181 void mlme_confirm_handler( const mac_api_t* api, mlme_primitive id, const void *data )
00182 {
00183     protocol_interface_info_entry_t *info_entry = protocol_stack_interface_info_get_by_id(api->parent_id);
00184     if (!info_entry) {
00185         return;
00186     }
00187     //TODO: create buffer_t and call correct function
00188     switch(id){
00189         case MLME_ASSOCIATE:{
00190             //Unsupported
00191             break;
00192         }
00193         case MLME_DISASSOCIATE:{
00194             //Unsupported
00195             break;
00196         }
00197         case MLME_GET:{
00198             mlme_get_conf_t *dat = (mlme_get_conf_t*)data;
00199             mac_mlme_get_confirmation_handler(info_entry, dat);
00200             break;
00201         }
00202         case MLME_GTS:{
00203             //Unsupported
00204             break;
00205         }
00206         case MLME_RESET:{
00207 //            mlme_reset_conf_t *dat = (mlme_reset_conf_t*)data;
00208             break;
00209         }
00210         case MLME_RX_ENABLE:{
00211             //Unsupported
00212             break;
00213         }
00214         case MLME_SCAN:{
00215             const mlme_scan_conf_t *dat = (mlme_scan_conf_t*)data;
00216             stop_bootstrap_timer(info_entry);
00217             info_entry->scan_cb(api->parent_id, dat);
00218             break;
00219         }
00220         case MLME_SET:{
00221 //            mlme_set_conf_t *dat = (mlme_set_conf_t*)data;
00222             break;
00223         }
00224         case MLME_START:{
00225 //            mlme_start_conf_t *dat = (mlme_start_conf_t*)data;
00226             stop_bootstrap_timer(info_entry);
00227             break;
00228         }
00229         case MLME_POLL:{
00230             const mlme_poll_conf_t *dat = (mlme_poll_conf_t*)data;
00231             mac_mlme_poll_confirm(info_entry, dat);
00232             break;
00233         }
00234         case MLME_BEACON_NOTIFY:
00235         case MLME_ORPHAN:
00236         case MLME_COMM_STATUS:
00237         case MLME_SYNC:
00238         case MLME_SYNC_LOSS:
00239         default:{
00240             tr_error("Invalid state in mlme_confirm_handler(): %d", id);
00241             break;
00242         }
00243     }
00244 }
00245 
00246 void mlme_indication_handler( const mac_api_t* api, mlme_primitive id, const void *data )
00247 {
00248     switch(id){
00249         case MLME_ASSOCIATE:{
00250             //Unsupported
00251             //mlme_associate_ind_t *dat = (mlme_associate_ind_t*)data;
00252             break;
00253         }
00254         case MLME_DISASSOCIATE:{
00255             //Unsupported
00256             //mlme_disassociate_ind_t *dat = (mlme_disassociate_ind_t*)data;
00257             break;
00258         }
00259         case MLME_BEACON_NOTIFY:{
00260             const mlme_beacon_ind_t *dat = (mlme_beacon_ind_t*)data;
00261             protocol_interface_info_entry_t *info_entry = protocol_stack_interface_info_get_by_id(api->parent_id);
00262             if( info_entry && info_entry->beacon_cb ){
00263                 info_entry->beacon_cb(api->parent_id, dat);
00264             }
00265             break;
00266         }
00267         case MLME_GTS:{
00268             //Unsupported
00269             break;
00270         }
00271         case MLME_ORPHAN:{
00272             //Unsupported
00273             break;
00274         }
00275         case MLME_COMM_STATUS:{
00276             mlme_comm_status_t *dat = (mlme_comm_status_t*)data;
00277             protocol_interface_info_entry_t *info_entry = protocol_stack_interface_info_get_by_id(api->parent_id);
00278             if( info_entry && info_entry->comm_status_ind_cb ){
00279                 info_entry->comm_status_ind_cb(api->parent_id, dat);
00280             }
00281 
00282             break;
00283         }
00284         case MLME_SYNC_LOSS:{
00285             mlme_sync_loss_t *dat = (mlme_sync_loss_t*)data;
00286             protocol_interface_info_entry_t *info_entry = protocol_stack_interface_info_get_by_id(api->parent_id);
00287             if (info_entry) {
00288                 if (dat->LossReason == BEACON_LOST) {
00289                     nwk_bootsrap_state_update(ARM_NWK_NWK_SCAN_FAIL, info_entry);
00290                 }
00291             }
00292             break;
00293         }
00294         case MLME_GET:
00295         case MLME_RESET:
00296         case MLME_RX_ENABLE:
00297         case MLME_SCAN:
00298         case MLME_SET:
00299         case MLME_START:
00300         case MLME_SYNC:
00301         case MLME_POLL:
00302         default:{
00303             tr_error("Invalid state in mlme_indication_handler(): %d", id);
00304             break;
00305         }
00306     }
00307 }
00308 
00309 bool mac_data_is_broadcast_addr(const sockaddr_t *addr)
00310 {
00311     return (addr->addr_type  == ADDR_802_15_4_SHORT ) &&
00312            (addr->address [2] == 0xFF && addr->address [3] == 0xFF);
00313 }
00314