cueOS  2.4
cueOS - Universal Show Control OS for ARM
osc_server.c
1/***============================================================================================================================
2 * Depedencies inclusion
3 * Necessary dependencies should be declared here. Header file should contain as little dependecies declarations as possible
4 *=============================================================================================================================*/
5
6#include "osc_server.h"
7#include "cueos_config.h"
8#include "cmsis_os.h"
9#include "lwip/udp.h"
10#include "ip.h"
11#include <string.h>
12#include <math.h>
13
14OSC_server_s osc_server = {
16};
17
18/***============================================================================================================================
19 * Private functions definitions
20 * These functions are only accessible from within the file's scope
21 *=============================================================================================================================*/
22
23
30static uint8_t _i_to_len(uint16_t i){
31 return floor(log10((double)i) + 1);
32}
33
40static uint8_t _roundup_4(uint8_t val){
41 return ((val + 4 - 1) / 4) * 4;
42}
43
51static void _osc_packet_cat(OSC_packet_s *osc_packet, const void *data, size_t size){
52 memmove(osc_packet->sp, data, size);
53 osc_packet->sp += size;
54}
55
62static void _osc_packet_roundup4(OSC_packet_s *osc_packet){
63
64 uint8_t cur_size = osc_packet->sp - osc_packet->data;
65 uint8_t new_size = _roundup_4(cur_size);
66
67 for(size_t i=0; i<(new_size - cur_size);i++){
68 _osc_packet_cat(osc_packet, OSC_PACKET_NULL_CHAR, 1);
69 }
70
71}
72
82static void _osc_server_receive(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port){
83 if(p!=NULL){
84 pbuf_free(p);
85 }
86}
87
88
89/***============================================================================================================================
90 * Public functions definitions
91 * These functions can be accessed outside of the file's scope
92 * @see osc_server.h for declarations
93 *=============================================================================================================================*/
94
102 osc_server._pcb = udp_new();
103 udp_bind(osc_server._pcb, IP_ADDR_ANY, cueOS_CONFIG_OSC_IN_PORT);
104 udp_recv(osc_server._pcb, _osc_server_receive, NULL);
105}
106
115void osc_packet_send(osc_media_type_e media_type, osc_control_type_e ctrl_type, uint16_t ctrl_id, uint32_t ctrl_val){
116
117 OSC_packet_s osc_packet;
118
119 uint32_t swapped_ctrl_val = ((ctrl_val>>24)&0xff) | ((ctrl_val<<8)&0xff0000) | ((ctrl_val>>8)&0xff00) | ((ctrl_val<<24)&0xff000000);
120 uint8_t uri_len = _i_to_len(media_type) + _i_to_len(ctrl_type) + _i_to_len(ctrl_id) + 3;
121 uint8_t tot_len = OSC_PACKET_PAYLOAD_LEN + _roundup_4(uri_len);
122
123 osc_packet.data = pvPortMalloc(tot_len * sizeof(uint8_t));
124 osc_packet.sp = osc_packet.data;
125
130 osc_packet.sp += sprintf(osc_packet.sp,OSC_PACKET_URI_FORMAT, media_type, ctrl_type, ctrl_id);
131 _osc_packet_roundup4(&osc_packet);
132 _osc_packet_cat(&osc_packet, ",i", 2);
133 _osc_packet_cat(&osc_packet, &swapped_ctrl_val, 4);
134 _osc_packet_roundup4(&osc_packet);
135
136 osc_server._p_tx = pbuf_alloc(PBUF_TRANSPORT, tot_len, PBUF_POOL);
137 pbuf_take(osc_server._p_tx, osc_packet.data, tot_len);
138 udp_sendto(osc_server._pcb, osc_server._p_tx, IP_ADDR_BROADCAST, cueOS_CONFIG_OSC_OUT_PORT);
139
140 vPortFree(osc_packet.data);
141
142 pbuf_free(osc_server._p_tx);
143
144}
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
void osc_packet_send(osc_media_type_e media_type, osc_control_type_e ctrl_type, uint16_t ctrl_id, uint32_t ctrl_val)
Prepares and send an OSC packet over UDP.
Definition: osc_server.c:115
void osc_server_init(void)
Initialises the OSC server instance.
Definition: osc_server.c:101
osc_control_type_e
List of OSC control types (As implemented by ASLS)
Definition: osc_server.h:42
osc_media_type_e
List of OSC-controllable media types (As implemented by ASLS)
Definition: osc_server.h:31
DEfines en OSC packet instance. TODO: this is not a RAM-efficient implementation.....
Definition: osc_server.h:52
char * sp
Definition: osc_server.h:54
char * data
Definition: osc_server.h:53
Defines an OSC server instance. Open Sound Control (OSC) is a protocol for networking sound synthesiz...
Definition: osc_server.h:63
struct pbuf * _p_tx
Definition: osc_server.h:65
struct udp_pcb * _pcb
Definition: osc_server.h:64
uint8_t is_initialised
Definition: osc_server.h:66