polypacket 1.1.21__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/env python3
2
+
3
+ from polypacket.protocol import *
4
+ from polypacket.polyservice import *
5
+ import signal
6
+ import argparse
7
+ import os
8
+ import time
9
+
10
+ args = None
11
+ parser = None
12
+ SERVICE : PolyService = None
13
+
14
+ def exit_handler(signum,frame):
15
+ print("Quit application. ")
16
+
17
+ if(SERVICE) :
18
+ SERVICE.close()
19
+ exit(0)
20
+
21
+ # Initialize the argument parser
22
+ def init_args():
23
+ global parser
24
+ parser = argparse.ArgumentParser("PolyPacket example in python")
25
+ parser.add_argument('-c', '--connection', type=str, help='Connection string', default="tcp:localhost:8020")
26
+ parser.add_argument('-f', '--file', type=str, help='protocol file', default="sample_protocol.yml")
27
+
28
+
29
+ def main():
30
+ global parser
31
+ global args
32
+ global SERVICE
33
+
34
+
35
+ #set up exit handler for ctrl+c
36
+ signal.signal(signal.SIGINT, exit_handler)
37
+
38
+ #Get arguments
39
+ init_args()
40
+ args = parser.parse_args()
41
+
42
+ inputFile = args.file
43
+
44
+ #parse input file
45
+ if os.path.isfile(inputFile):
46
+ SERVICE = PolyService(inputFile)
47
+ else :
48
+ print("Unable to read input file: " + inputFile)
49
+ return 1
50
+
51
+ #set handler for 'Data' type packets
52
+ SERVICE.handlers['Data'] = data_handler
53
+
54
+ #set service prints to go to terminal
55
+ SERVICE.print = print
56
+
57
+ #attempt connection
58
+ SERVICE.connect(args.connection)
59
+
60
+ while not SERVICE.isConnected():
61
+ time.sleep(1)
62
+ timeout -=1
63
+ if timeout <= 0:
64
+ print("Connection Timedout, check connection string")
65
+ exit(1)
66
+
67
+ #Send a packet
68
+ SERVICE.sendPacket("SendCmd", {"src" : 0x01, "dst": 0x02, "cmd": "led_ON"})
69
+
70
+
71
+
72
+
73
+
74
+ def data_handler(service : PolyService, req : PolyPacket, resp : PolyPacket):
75
+
76
+ #get field data
77
+ sensorName = req.getField('sensorName')
78
+
79
+ print( "Message from {0}: {1}".format(sensorName, req.toJSON))
80
+
81
+
82
+ if __name__ == '__main__':
83
+ main()
@@ -0,0 +1,47 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <Protocol name="Sample" namespace="sp"
3
+ desc="This is a sample protocol made up to demonstrate features of the PolyPacket code generation tool. The idea
4
+ is to have a tool that can automatically create parseable/serializable messaging for embedded systems.">
5
+ <!--First we declare all Field descriptors-->
6
+ <Fields>
7
+
8
+ <!--Common -->
9
+ <Field name="cmd" type="enum" format="hex" desc="command byte for controlling node" >
10
+ <Val name="led_ON" desc="turns on led" />
11
+ <Val name="led_OFF" desc="turns off led" />
12
+ <Val name="reset" desc="resets the device" />
13
+ </Field>
14
+
15
+ <!-- SensorData -->
16
+ <Field name="sensorA" type="int16" format="dec" desc="Value of Sensor A"/>
17
+ <Field name="sensorB" type="int" format="dec" desc="Value of Sensor B" />
18
+ <Field name="sensorName" type="string[32]" format="ascii" desc="Name of sensor"/>
19
+
20
+ </Fields>
21
+
22
+ <!--Declare all Packet Types-->
23
+ <Packets>
24
+ <Packet name="SendCmd" desc="Message to send command to node" >
25
+ <Field name="cmd" req="true"/>
26
+ </Packet>
27
+
28
+ <Packet name="GetData" desc="Message to get data from node" response="Data"/>
29
+
30
+ <Packet name="Data" desc="Message containing data from sensor" >
31
+ <Field name="sensorA"/>
32
+ <Field name="sensorB"/>
33
+ <!-- Adding a description here will overwrite the description in documentation for this packet type -->
34
+ <Field name="sensorName" desc="Name of sensor responding to message "/>
35
+ </Packet>
36
+
37
+ </Packets>
38
+
39
+ <Structs>
40
+ <Struct name="node" desc="struct for modeling node">
41
+ <Field name="sensorA"/>
42
+ <Field name="sensorB"/>
43
+ <Field name="sensorName" desc="Name of sensor responding to message "/>
44
+ </Struct>
45
+ </Structs>
46
+
47
+ </Protocol>
@@ -0,0 +1,118 @@
1
+ ---
2
+ name: sample
3
+ namespace: sp #this defines the namespace used for functions and types in the code. This allows multiple protocols to be used in a project
4
+ desc: This is a sample protocol made up to demonstrate features of the PolyPacket
5
+ code generation tool. The idea is to have a tool that can automatically create parseable/serializable
6
+ messaging for embedded systems
7
+
8
+ ###########################################################################################################
9
+ # FIELDS #
10
+ ###########################################################################################################
11
+
12
+ fields:
13
+
14
+ #Fields can be nested into a 'Field Group' for convenience. They will be put in the packet just like regular fields
15
+ - header:
16
+ - src: {type: uint16, desc: Address of node sending message }
17
+ - dst: {type: uint16, desc: Address of node to receive message }
18
+
19
+ - sensorA: { type: int16 ,desc: Value of Sensor A} #Simple Fields can be defined as inline dictionares to save space
20
+
21
+ - sensorB:
22
+ type: int
23
+ desc: Value of Sensor B
24
+
25
+ - sensorName:
26
+ type: string
27
+ desc: Name of sensor
28
+
29
+ - cmd:
30
+ type: enum
31
+ format: hex
32
+ desc: command byte for controlling node
33
+ vals:
34
+ - led_ON: { desc: turns on led}
35
+ - led_OFF: { desc: turns off led}
36
+ - reset: { desc: resets device }
37
+
38
+ ###########################################################################################################
39
+ # Packets #
40
+ ###########################################################################################################
41
+ packets:
42
+ - SendCmd:
43
+ desc: Message to send command to node
44
+ fields:
45
+ - header
46
+ - cmd
47
+
48
+ - GetData:
49
+ desc: Message tp get data from node
50
+ response: Data #A response packet can be specified
51
+ fields:
52
+ - header
53
+
54
+ - SetData:
55
+ desc: contains data from a sensor
56
+ fields:
57
+ - header
58
+ - sensorA
59
+ - sensorB
60
+ - sensorName : {desc: Name of sensor sending data } #Field descriptions can be overriden for different packets
61
+
62
+ - Data:
63
+ desc: contains data from a sensor
64
+ fields:
65
+ - header
66
+ - sensorA
67
+ - sensorB
68
+ - sensorName : {desc: Name of sensor sending data } #Field descriptions can be overriden for different packets
69
+ ###########################################################################################################
70
+ # Structs #
71
+ ###########################################################################################################
72
+
73
+ structs:
74
+ - Node:
75
+ desc: struct for modeling node
76
+ fields:
77
+ - sensorA
78
+ - sensorB
79
+ - sensorName
80
+
81
+ ###########################################################################################################
82
+ # Agents #
83
+ ###########################################################################################################
84
+ agents:
85
+ # This creates an agent named 'node' to load it, add '-a node' when running poly packet
86
+ # naming a sim 'default' will cause it to load
87
+ - node:
88
+ # init signature is init(service):
89
+ # There is a global dicst named DataStore that can be used to store variables
90
+ init: |
91
+ DataStore['node'] = service.newStruct('Node')
92
+ DataStore['node'].setField('sensorName', 'node01')
93
+ DataStore['node'].setField('sensorA', 25)
94
+ DataStore['node'].setField('sensorB', 65)
95
+ node = DataStore['node']
96
+ service.print('\nCreating Sensor node:\n name: {0}\n sensorA: {1}\n sensorB: {2}\n'.format(node.getField('sensorName'),node.getField('sensorA'),node.getField('sensorB') ))
97
+
98
+
99
+ #handlers fill out a function with the signature <name>_handler(service, req, resp):
100
+ # you can print out to the console with service.print(text)
101
+ handlers:
102
+
103
+ #Use packets/nodes can be copied to eachother. All shared fields that are present in the source will get copied to the destination
104
+ - SetData: |
105
+ req.copyTo(DataStore['node'])
106
+
107
+ - GetData: |
108
+ DataStore['node'].copyTo(resp)
109
+
110
+ #You can add custom commands to a sim that will be loaded in for autocomplete and help menus in the CLI
111
+ commands:
112
+ - rename:
113
+ desc: renames the node
114
+ args:
115
+ - name: {desc: new name for node, default: 'node01'}
116
+ handler: |
117
+ DataStore['node'].setField('sensorName', args['name'])
118
+ service.print('\nRenaming Sensor node:\n name: {0}\n'.format(name))
@@ -0,0 +1,3 @@
1
+ ---
2
+
3
+ redirect: https://gitlab.com/mrt-public/modules/Utilities/OTA/-/raw/main/poly/ota-protocol.yml
@@ -0,0 +1,223 @@
1
+ /**
2
+ *@file app_${proto.name.lower()}.c
3
+ *@brief generated protocol source code
4
+ *@author make_protocol.py
5
+ *@date ${proto.genTime}
6
+ */
7
+
8
+ /***********************************************************
9
+ Application Layer
10
+ ***********************************************************/
11
+
12
+ #include "app_${proto.name.lower()}.h"
13
+ %if proto.genUtility:
14
+ #include "platforms/linux/linux_uart.h"
15
+ #include "platforms/linux/linux_udp.h"
16
+
17
+ int ifaceMode;
18
+ udp_stream udp;
19
+ int fd;
20
+
21
+ #define ${proto.namespace.upper()}_PRINT(packet) fp_print_json((packet),printBuf); printf("%s",printBuf)
22
+ %else:
23
+ mrt_uart_handle_t ifac0;
24
+ %endif
25
+ static uint8_t iface0_rx_buf[512];
26
+ static char printBuf[512];
27
+
28
+ static inline HandlerStatus_e iface0_write(uint8_t* data, int len)
29
+ {
30
+ /* Place code for writing bytes on interface 0 here */
31
+ %if proto.genUtility:
32
+ switch (ifaceMode)
33
+ {
34
+ case UART_MODE:
35
+ uart_write(fd,data,len);
36
+ break;
37
+ case UDP_MODE:
38
+ udp_send(&udp,data,len);
39
+ break;
40
+ }
41
+ %else:
42
+ MRT_UART_TX(ifac0, data, len, 10);
43
+ %endif
44
+
45
+ return PACKET_SENT;
46
+ }
47
+
48
+
49
+ static inline void iface0_read()
50
+ {
51
+ int len =0;
52
+ /* Place code for reading bytes from interface 0 here */
53
+ %if proto.genUtility:
54
+ switch (ifaceMode)
55
+ {
56
+ case UART_MODE:
57
+ len = uart_read(fd,iface0_rx_buf, 32);
58
+ break;
59
+ case UDP_MODE:
60
+ len = udp_recv(&udp,iface0_rx_buf,512);
61
+ break;
62
+ }
63
+
64
+ %else:
65
+ //TODO read bytes from interface to iface0_rx_buf
66
+ len = MRT_UART_RX(ifac0, iface0_rx_buf, 32, 5); //read 32 bytes at a time
67
+ %endif
68
+
69
+ ${proto.namespace}_service_feed(0,iface0_rx_buf, len);
70
+ }
71
+
72
+ /*******************************************************************************
73
+ App Init/end
74
+ *******************************************************************************/
75
+ %if proto.genUtility:
76
+ void app_${proto.name.lower()}_init(const char* connectionStr, int mode)
77
+ {
78
+ /* initialize peripheral for iface_0 */
79
+ ifaceMode = mode;
80
+
81
+ switch(ifaceMode)
82
+ {
83
+ case UART_MODE:
84
+ if(uart_init(&fd, connectionStr))
85
+ printf("successfully opened port: %s\n",connectionStr);
86
+ else
87
+ printf("Could not open port: %s",connectionStr);
88
+ break;
89
+ case UDP_MODE:
90
+ udp_init(&udp,connectionStr);
91
+ break;
92
+ }
93
+
94
+ %else:
95
+ void app_${proto.name.lower()}_init(mrt_uart_handle_t uart_handle)
96
+ {
97
+ /* Set ifac0 to uart handle, this can use any peripheral, but uart is the most common case */
98
+ ifac0 = uart_handle; //set interface to uart handle
99
+ %endif
100
+
101
+ //initialize service
102
+ ${proto.namespace}_service_init(1,16);
103
+
104
+ ${proto.namespace}_service_register_bytes_tx(0, iface0_write);
105
+
106
+ }
107
+
108
+ void app_${proto.name.lower()}_end()
109
+ {
110
+ %if proto.genUtility:
111
+ switch(ifaceMode)
112
+ {
113
+ case UART_MODE:
114
+ uart_close(fd);
115
+ break;
116
+ case UDP_MODE:
117
+ udp_close(&udp);
118
+ break;
119
+ }
120
+ %endif
121
+ }
122
+
123
+ /*******************************************************************************
124
+ App Process
125
+ *******************************************************************************/
126
+
127
+ void app_${proto.name.lower()}_process()
128
+ {
129
+ /* read in new data from iface 0*/
130
+ iface0_read();
131
+
132
+ /* process the actual service */
133
+ ${proto.namespace}_service_process();
134
+
135
+ }
136
+
137
+
138
+ /*******************************************************************************
139
+ Packet handlers
140
+ *******************************************************************************/
141
+ % for packet in proto.packets:
142
+ %if not packet.standard:
143
+ %if not packet.hasResponse:
144
+ /**
145
+ *@brief Handler for receiving ${packet.name} packets
146
+ *@param ${packet.name} incoming ${packet.name} packet
147
+ *@return handling ${proto.namespace}_status
148
+ */
149
+ HandlerStatus_e ${proto.namespace}_${packet.camel()}_handler(${proto.namespace}_packet_t* ${proto.namespace}_${packet.name})
150
+ %else:
151
+ /**
152
+ *@brief Handler for receiving ${packet.name} packets
153
+ *@param ${packet.name} incoming ${packet.name} packet
154
+ *@param ${packet.response.name} ${packet.response.name} packet to respond with
155
+ *@return handling ${proto.namespace}_status
156
+ */
157
+ HandlerStatus_e ${proto.namespace}_${packet.camel()}_handler(${proto.namespace}_packet_t* ${proto.namespace}_${packet.name}, ${proto.namespace}_packet_t* ${proto.namespace}_${packet.response.name})
158
+ %endif
159
+ {
160
+ /* Get Required Fields in packet */
161
+ % for field in packet.fields:
162
+ ${field.getDeclaration()}; //${field.desc}
163
+ %endfor
164
+
165
+ % for field in packet.fields:
166
+ %if field.isArray:
167
+ ${proto.namespace}_get${field.camel()}(${proto.namespace}_${packet.name}, ${field.name});
168
+ %else:
169
+ ${field.name} = ${proto.namespace}_get${field.camel()}(${proto.namespace}_${packet.name});
170
+ %endif
171
+ % endfor
172
+
173
+ % for field in packet.fields:
174
+ % if field.isEnum:
175
+ switch(${field.name})
176
+ {
177
+ % for val in field.vals:
178
+ case ${proto.namespace.upper()+"_"+field.name.upper() + "_" + val.name.upper()}: // ${val.desc}
179
+ break;
180
+ % endfor
181
+ default:
182
+ break;
183
+ }
184
+
185
+ %endif
186
+ % endfor
187
+ % for field in packet.fields:
188
+ % if field.isMask:
189
+ % for val in field.vals:
190
+ if(${field.name} & ${proto.namespace.upper()+"_"+field.name.upper() + "_" + val.name.upper()}) // ${val.desc}
191
+ {
192
+ }
193
+ %endfor
194
+
195
+ %endif
196
+ % endfor
197
+ %if packet.hasResponse:
198
+ /* Set required Fields in response */
199
+ % for field in packet.response.fields:
200
+ //${proto.namespace}_set${field.camel()}(${proto.namespace}_${packet.response.name}, value ); //${field.desc}
201
+ %endfor
202
+ %endif
203
+
204
+
205
+
206
+ return PACKET_NOT_HANDLED;
207
+ }
208
+
209
+ %endif
210
+ % endfor
211
+
212
+ /**
213
+ *@brief catch-all handler for any packet not handled by its default handler
214
+ *@param metaPacket ptr to ${proto.namespace}_packet_t containing packet
215
+ *@param ${proto.namespace}_response ptr to response
216
+ *@return handling ${proto.namespace}_status
217
+ */
218
+ HandlerStatus_e ${proto.namespace}_default_handler( ${proto.namespace}_packet_t * ${proto.namespace}_packet, ${proto.namespace}_packet_t * ${proto.namespace}_response)
219
+ {
220
+
221
+
222
+ return PACKET_NOT_HANDLED;
223
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ *@file app_${proto.name.lower()}.h
3
+ *@brief generated protocol source code
4
+ *@author make_protocol.py
5
+ *@date ${proto.genTime}
6
+ */
7
+
8
+ #include "${proto.fileName}.h"
9
+ %if not proto.genUtility:
10
+ #include "platforms/common/mrt_platform.h"
11
+ %endif
12
+
13
+
14
+ /**
15
+ *@brief Initialize the packet service
16
+ */
17
+ %if proto.genUtility:
18
+
19
+ #define UART_MODE 0
20
+ #define UDP_MODE 1
21
+
22
+ void app_${proto.name.lower()}_init(const char* connectionStr, int mode);
23
+ %else:
24
+ void app_${proto.name.lower()}_init(mrt_uart_handle_t uart_handle);
25
+ %endif
26
+
27
+ /**
28
+ *@brief ends service
29
+ */
30
+ void app_${proto.name.lower()}_end();
31
+
32
+ /**
33
+ *@brief process the data for the packet service
34
+ */
35
+ void app_${proto.name.lower()}_process();