1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation 2425bb815Sopenharmony_ci * 3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License. 5425bb815Sopenharmony_ci * You may obtain a copy of the License at 6425bb815Sopenharmony_ci * 7425bb815Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8425bb815Sopenharmony_ci * 9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS 11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and 13425bb815Sopenharmony_ci * limitations under the License. 14425bb815Sopenharmony_ci */ 15425bb815Sopenharmony_ci 16425bb815Sopenharmony_ci#include "jerryscript-ext/debugger.h" 17425bb815Sopenharmony_ci#include "jext-common.h" 18425bb815Sopenharmony_ci 19425bb815Sopenharmony_ci#if defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) 20425bb815Sopenharmony_ci 21425bb815Sopenharmony_ci/* A simplified transmission layer. */ 22425bb815Sopenharmony_ci 23425bb815Sopenharmony_ci/** 24425bb815Sopenharmony_ci * Size of the raw packet header. 25425bb815Sopenharmony_ci */ 26425bb815Sopenharmony_ci#define JERRYX_DEBUGGER_RAWPACKET_HEADER_SIZE 1 27425bb815Sopenharmony_ci/** 28425bb815Sopenharmony_ci * Maximum message size with 1 byte size field. 29425bb815Sopenharmony_ci */ 30425bb815Sopenharmony_ci#define JERRYX_DEBUGGER_RAWPACKET_ONE_BYTE_LEN_MAX 255 31425bb815Sopenharmony_ci 32425bb815Sopenharmony_ci/** 33425bb815Sopenharmony_ci * Header for incoming packets. 34425bb815Sopenharmony_ci */ 35425bb815Sopenharmony_citypedef struct 36425bb815Sopenharmony_ci{ 37425bb815Sopenharmony_ci uint8_t size; /**< size of the message */ 38425bb815Sopenharmony_ci} jerryx_rawpacket_receive_header_t; 39425bb815Sopenharmony_ci 40425bb815Sopenharmony_ci/** 41425bb815Sopenharmony_ci * Close a tcp connection. 42425bb815Sopenharmony_ci */ 43425bb815Sopenharmony_cistatic void 44425bb815Sopenharmony_cijerryx_debugger_rp_close (jerry_debugger_transport_header_t *header_p) /**< header for the transport interface */ 45425bb815Sopenharmony_ci{ 46425bb815Sopenharmony_ci JERRYX_ASSERT (!jerry_debugger_transport_is_connected ()); 47425bb815Sopenharmony_ci 48425bb815Sopenharmony_ci jerry_heap_free ((void *) header_p, sizeof (jerry_debugger_transport_header_t)); 49425bb815Sopenharmony_ci} /* jerryx_debugger_rp_close */ 50425bb815Sopenharmony_ci 51425bb815Sopenharmony_ci/** 52425bb815Sopenharmony_ci * Send data over a simple raw packet connection. 53425bb815Sopenharmony_ci * 54425bb815Sopenharmony_ci * @return true - if the data has been sent successfully 55425bb815Sopenharmony_ci * false - otherwise 56425bb815Sopenharmony_ci */ 57425bb815Sopenharmony_cistatic bool 58425bb815Sopenharmony_cijerryx_debugger_rp_send (jerry_debugger_transport_header_t *header_p, /**< header for the transport interface */ 59425bb815Sopenharmony_ci uint8_t *message_p, /**< message to be sent */ 60425bb815Sopenharmony_ci size_t message_length) /**< message length in bytes */ 61425bb815Sopenharmony_ci{ 62425bb815Sopenharmony_ci JERRYX_ASSERT (message_length <= JERRYX_DEBUGGER_RAWPACKET_ONE_BYTE_LEN_MAX); 63425bb815Sopenharmony_ci 64425bb815Sopenharmony_ci message_p[-1] = (uint8_t) message_length; 65425bb815Sopenharmony_ci 66425bb815Sopenharmony_ci return header_p->next_p->send (header_p->next_p, message_p - 1, message_length + 1); 67425bb815Sopenharmony_ci} /* jerryx_debugger_rp_send */ 68425bb815Sopenharmony_ci 69425bb815Sopenharmony_ci/** 70425bb815Sopenharmony_ci * Receive data from a rawpacket connection. 71425bb815Sopenharmony_ci * 72425bb815Sopenharmony_ci * @return true - if data has been received successfully 73425bb815Sopenharmony_ci * false - otherwise 74425bb815Sopenharmony_ci */ 75425bb815Sopenharmony_cistatic bool 76425bb815Sopenharmony_cijerryx_debugger_rp_receive (jerry_debugger_transport_header_t *header_p, /**< header for the transport interface */ 77425bb815Sopenharmony_ci jerry_debugger_transport_receive_context_t *receive_context_p) /**< receive context */ 78425bb815Sopenharmony_ci{ 79425bb815Sopenharmony_ci if (!header_p->next_p->receive (header_p->next_p, receive_context_p)) 80425bb815Sopenharmony_ci { 81425bb815Sopenharmony_ci return false; 82425bb815Sopenharmony_ci } 83425bb815Sopenharmony_ci 84425bb815Sopenharmony_ci if (receive_context_p->message_p == NULL) 85425bb815Sopenharmony_ci { 86425bb815Sopenharmony_ci return true; 87425bb815Sopenharmony_ci } 88425bb815Sopenharmony_ci 89425bb815Sopenharmony_ci size_t message_total_length = receive_context_p->message_total_length; 90425bb815Sopenharmony_ci 91425bb815Sopenharmony_ci if (message_total_length == 0) 92425bb815Sopenharmony_ci { 93425bb815Sopenharmony_ci /* Byte stream. */ 94425bb815Sopenharmony_ci if (receive_context_p->message_length < sizeof (jerryx_rawpacket_receive_header_t)) 95425bb815Sopenharmony_ci { 96425bb815Sopenharmony_ci receive_context_p->message_p = NULL; 97425bb815Sopenharmony_ci return true; 98425bb815Sopenharmony_ci } 99425bb815Sopenharmony_ci } 100425bb815Sopenharmony_ci else 101425bb815Sopenharmony_ci { 102425bb815Sopenharmony_ci /* Datagram packet. */ 103425bb815Sopenharmony_ci JERRYX_ASSERT (receive_context_p->message_length >= sizeof (jerryx_rawpacket_receive_header_t)); 104425bb815Sopenharmony_ci } 105425bb815Sopenharmony_ci 106425bb815Sopenharmony_ci uint8_t *message_p = receive_context_p->message_p; 107425bb815Sopenharmony_ci size_t message_length = (size_t) (message_p[0]); 108425bb815Sopenharmony_ci 109425bb815Sopenharmony_ci if (message_total_length == 0) 110425bb815Sopenharmony_ci { 111425bb815Sopenharmony_ci size_t new_total_length = message_length + sizeof (jerryx_rawpacket_receive_header_t); 112425bb815Sopenharmony_ci 113425bb815Sopenharmony_ci /* Byte stream. */ 114425bb815Sopenharmony_ci if (receive_context_p->message_length < new_total_length) 115425bb815Sopenharmony_ci { 116425bb815Sopenharmony_ci receive_context_p->message_p = NULL; 117425bb815Sopenharmony_ci return true; 118425bb815Sopenharmony_ci } 119425bb815Sopenharmony_ci 120425bb815Sopenharmony_ci receive_context_p->message_total_length = new_total_length; 121425bb815Sopenharmony_ci } 122425bb815Sopenharmony_ci else 123425bb815Sopenharmony_ci { 124425bb815Sopenharmony_ci /* Datagram packet. */ 125425bb815Sopenharmony_ci JERRYX_ASSERT (receive_context_p->message_length == (message_length + sizeof (jerryx_rawpacket_receive_header_t))); 126425bb815Sopenharmony_ci } 127425bb815Sopenharmony_ci 128425bb815Sopenharmony_ci receive_context_p->message_p = message_p + sizeof (jerryx_rawpacket_receive_header_t); 129425bb815Sopenharmony_ci receive_context_p->message_length = message_length; 130425bb815Sopenharmony_ci 131425bb815Sopenharmony_ci return true; 132425bb815Sopenharmony_ci} /* jerryx_debugger_rp_receive */ 133425bb815Sopenharmony_ci 134425bb815Sopenharmony_ci/** 135425bb815Sopenharmony_ci * Initialize a simple raw packet transmission layer. 136425bb815Sopenharmony_ci * 137425bb815Sopenharmony_ci * @return true - if the connection succeeded 138425bb815Sopenharmony_ci * false - otherwise 139425bb815Sopenharmony_ci */ 140425bb815Sopenharmony_cibool 141425bb815Sopenharmony_cijerryx_debugger_rp_create (void) 142425bb815Sopenharmony_ci{ 143425bb815Sopenharmony_ci const size_t interface_size = sizeof (jerry_debugger_transport_header_t); 144425bb815Sopenharmony_ci jerry_debugger_transport_header_t *header_p; 145425bb815Sopenharmony_ci header_p = (jerry_debugger_transport_header_t *) jerry_heap_alloc (interface_size); 146425bb815Sopenharmony_ci 147425bb815Sopenharmony_ci if (!header_p) 148425bb815Sopenharmony_ci { 149425bb815Sopenharmony_ci return false; 150425bb815Sopenharmony_ci } 151425bb815Sopenharmony_ci 152425bb815Sopenharmony_ci header_p->close = jerryx_debugger_rp_close; 153425bb815Sopenharmony_ci header_p->send = jerryx_debugger_rp_send; 154425bb815Sopenharmony_ci header_p->receive = jerryx_debugger_rp_receive; 155425bb815Sopenharmony_ci 156425bb815Sopenharmony_ci jerry_debugger_transport_add (header_p, 157425bb815Sopenharmony_ci JERRYX_DEBUGGER_RAWPACKET_HEADER_SIZE, 158425bb815Sopenharmony_ci JERRYX_DEBUGGER_RAWPACKET_ONE_BYTE_LEN_MAX, 159425bb815Sopenharmony_ci JERRYX_DEBUGGER_RAWPACKET_HEADER_SIZE, 160425bb815Sopenharmony_ci JERRYX_DEBUGGER_RAWPACKET_ONE_BYTE_LEN_MAX); 161425bb815Sopenharmony_ci 162425bb815Sopenharmony_ci return true; 163425bb815Sopenharmony_ci} /* jerryx_debugger_rp_create */ 164425bb815Sopenharmony_ci 165425bb815Sopenharmony_ci#else /* !(defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1)) */ 166425bb815Sopenharmony_ci 167425bb815Sopenharmony_ci/** 168425bb815Sopenharmony_ci * Dummy function when debugger is disabled. 169425bb815Sopenharmony_ci * 170425bb815Sopenharmony_ci * @return false 171425bb815Sopenharmony_ci */ 172425bb815Sopenharmony_cibool 173425bb815Sopenharmony_cijerryx_debugger_rp_create (void) 174425bb815Sopenharmony_ci{ 175425bb815Sopenharmony_ci return false; 176425bb815Sopenharmony_ci} /* jerryx_debugger_rp_create */ 177425bb815Sopenharmony_ci 178425bb815Sopenharmony_ci#endif /* defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) */ 179