Last active
April 10, 2018 11:21
-
-
Save nrbrook/4546b329bf510838d5681422c1feb80d to your computer and use it in GitHub Desktop.
A python script to extract the events from SI Labs native_gecko.h into an event loop handler that calls weak functions, allowing you to define event handler functions with parameters instead of a big switch statement with an event data struct.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import re | |
native_gecko_file = open("/Applications/Simplicity Studio.app/Contents/Eclipse/developer/sdks/gecko_sdk_suite/v2.2/protocol/bluetooth/ble_stack/inc/soc/native_gecko.h", "r") | |
contents = native_gecko_file.read() | |
native_gecko_file.close() | |
pattern = re.compile(r'gecko_evt_(.*?)_id\s') | |
c_string = "" | |
h_string = "" | |
for (event) in re.findall(pattern, contents): | |
c_string += r""" | |
case gecko_evt_{0}_id: {{""".format(event) | |
h_string += r""" | |
__attribute__ ((weak)) void gecko_evt_{0}(""".format(event) | |
event_params_pattern = re.compile(r'gecko_msg_%s_evt_t\n\{\n(.*?)\n\}\);' % event, re.MULTILINE | re.DOTALL) | |
match = re.search(event_params_pattern, contents) | |
if match: | |
c_string += r""" | |
struct gecko_msg_{0}_evt_t* data = &evt->data.evt_{0}; | |
gecko_evt_{0}(""".format(event) | |
event_param_pattern = re.compile(r'\s+(.*?)\s+(.*?);') | |
for (typename, param) in re.findall(event_param_pattern, match.group(1)): | |
c_string += "data->%s, " % param | |
h_string += "{0} {1}, ".format(typename, param) | |
c_string = c_string[:-2] | |
h_string = h_string[:-2] | |
else: | |
c_string += r""" | |
gecko_evt_{0}(""".format(event) | |
h_string += "void" | |
c_string += r"""); | |
} break;""" | |
h_string += ");" | |
gecko_weak_c_file = open("gecko_weak.c", "w+") | |
gecko_weak_c_file.write(r"""/***************************************************************************** | |
* @file gecko_weak.c | |
* @brief Gecko weak event functions | |
* @author Nick Brook | |
* @version 1.00 | |
****************************************************************************** | |
* @section License | |
* <b>(C) Copyright 2017 Nick Brook, https://www.nickbrook.me</b> | |
******************************************************************************* | |
* | |
* Permission is granted to anyone to use this software for any purpose, | |
* including commercial applications, and to alter it and redistribute it | |
* freely, subject to the following restrictions: | |
* | |
* 1. The origin of this software must not be misrepresented; you must not | |
* claim that you wrote the original software. | |
* 2. Altered source versions must be plainly marked as such, and must not be | |
* misrepresented as being the original software. | |
* 3. This notice may not be removed or altered from any source distribution. | |
* | |
* DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Nick Brook has no | |
* obligation to support this Software. Nick Brook is providing the | |
* Software "AS IS", with no express or implied warranties of any kind, | |
* including, but not limited to, any implied warranties of merchantability | |
* or fitness for any particular purpose or warranties against infringement | |
* of any proprietary rights of a third party. | |
* | |
* Nick Brook will not be liable for any consequential, incidental, or | |
* special damages, or any other relief, or for any claim by any third party, | |
* arising from your use of this Software. | |
* | |
******************************************************************************/ | |
#include "gecko_weak.h" | |
#include "gecko_weak_handler.h" | |
#include "native_gecko.h" | |
void gecko_evt_handle(struct gecko_cmd_packet* evt) {{ | |
switch (BGLIB_MSG_ID(evt->header)) {{{0} | |
}} | |
}} | |
void gecko_evt_loop() {{ | |
while (1) {{ | |
/* Event pointer for handling events */ | |
struct gecko_cmd_packet* evt; | |
/* Check for stack event. */ | |
evt = gecko_wait_event(); | |
gecko_evt_handle(evt); | |
}} | |
}} | |
""".format(c_string)) | |
gecko_weak_c_file.close() | |
gecko_weak_h_file = open("gecko_weak.h", "w+") | |
gecko_weak_h_file.write(r"""/***************************************************************************** | |
* @file gecko_weak.h | |
* @brief Gecko weak event functions | |
* @author Nick Brook | |
* @version 1.00 | |
****************************************************************************** | |
* @section License | |
* <b>(C) Copyright 2017 Nick Brook, https://www.nickbrook.me</b> | |
******************************************************************************* | |
* | |
* Permission is granted to anyone to use this software for any purpose, | |
* including commercial applications, and to alter it and redistribute it | |
* freely, subject to the following restrictions: | |
* | |
* 1. The origin of this software must not be misrepresented; you must not | |
* claim that you wrote the original software. | |
* 2. Altered source versions must be plainly marked as such, and must not be | |
* misrepresented as being the original software. | |
* 3. This notice may not be removed or altered from any source distribution. | |
* | |
* DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Nick Brook has no | |
* obligation to support this Software. Nick Brook is providing the | |
* Software "AS IS", with no express or implied warranties of any kind, | |
* including, but not limited to, any implied warranties of merchantability | |
* or fitness for any particular purpose or warranties against infringement | |
* of any proprietary rights of a third party. | |
* | |
* Nick Brook will not be liable for any consequential, incidental, or | |
* special damages, or any other relief, or for any claim by any third party, | |
* arising from your use of this Software. | |
* | |
******************************************************************************/ | |
#ifndef GECKO_WEAK_H_ | |
#define GECKO_WEAK_H_ | |
#include "native_gecko.h" | |
{0} | |
#endif /* GECKO_WEAK_H_ */ | |
""".format(h_string)) | |
gecko_weak_h_file.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/***************************************************************************** | |
* @file gecko_weak_handler.h | |
* @brief Gecko weak event handlers | |
* @author Nick Brook | |
* @version 1.00 | |
****************************************************************************** | |
* @section License | |
* <b>(C) Copyright 2017 Nick Brook, https://www.nickbrook.me</b> | |
******************************************************************************* | |
* | |
* Permission is granted to anyone to use this software for any purpose, | |
* including commercial applications, and to alter it and redistribute it | |
* freely, subject to the following restrictions: | |
* | |
* 1. The origin of this software must not be misrepresented; you must not | |
* claim that you wrote the original software. | |
* 2. Altered source versions must be plainly marked as such, and must not be | |
* misrepresented as being the original software. | |
* 3. This notice may not be removed or altered from any source distribution. | |
* | |
* DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Nick Brook has no | |
* obligation to support this Software. Nick Brook is providing the | |
* Software "AS IS", with no express or implied warranties of any kind, | |
* including, but not limited to, any implied warranties of merchantability | |
* or fitness for any particular purpose or warranties against infringement | |
* of any proprietary rights of a third party. | |
* | |
* Nick Brook will not be liable for any consequential, incidental, or | |
* special damages, or any other relief, or for any claim by any third party, | |
* arising from your use of this Software. | |
* | |
******************************************************************************/ | |
#ifndef GECKO_WEAK_HANDLER_H_ | |
#define GECKO_WEAK_HANDLER_H_ | |
#include "native_gecko.h" | |
#include "gecko_weak.h" | |
void gecko_evt_handle(struct gecko_cmd_packet* evt); | |
void gecko_evt_loop(); | |
#endif /* GECKO_WEAK_HANDLER_H_ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment