Path: blob/21.2-virgl/src/gallium/auxiliary/rbug/rbug_core.c
4561 views
/*1* Copyright 2009 VMware, Inc.2* All Rights Reserved.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* on the rights to use, copy, modify, merge, publish, distribute, sub8* license, and/or sell copies of the Software, and to permit persons to whom9* the Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL18* VMWARE AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,19* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR20* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE21* USE OR OTHER DEALINGS IN THE SOFTWARE.22*/2324/*25* This file holds the function implementation for one of the rbug extensions.26* Prototypes and declerations of functions and structs is in the same folder27* in the header file matching this file's name.28*29* The functions starting rbug_send_* encodes a call to the write format and30* sends that to the supplied connection, while functions starting with31* rbug_demarshal_* demarshal data in the wire protocol.32*33* Functions ending with _reply are replies to requests.34*/3536#include "rbug_internal.h"37#include "rbug_core.h"3839int rbug_send_noop(struct rbug_connection *__con,40uint32_t *__serial)41{42uint32_t __len = 0;43uint32_t __pos = 0;44uint8_t *__data = NULL;45int __ret = 0;4647LEN(8); /* header */4849/* align */50PAD(__len, 8);5152__data = (uint8_t*)MALLOC(__len);53if (!__data)54return -ENOMEM;5556WRITE(4, int32_t, ((int32_t)RBUG_OP_NOOP));57WRITE(4, uint32_t, ((uint32_t)(__len / 4)));5859/* final pad */60PAD(__pos, 8);6162if (__pos != __len) {63__ret = -EINVAL;64} else {65rbug_connection_send_start(__con, RBUG_OP_NOOP, __len);66rbug_connection_write(__con, __data, __len);67__ret = rbug_connection_send_finish(__con, __serial);68}6970FREE(__data);71return __ret;72}7374int rbug_send_ping(struct rbug_connection *__con,75uint32_t *__serial)76{77uint32_t __len = 0;78uint32_t __pos = 0;79uint8_t *__data = NULL;80int __ret = 0;8182LEN(8); /* header */8384/* align */85PAD(__len, 8);8687__data = (uint8_t*)MALLOC(__len);88if (!__data)89return -ENOMEM;9091WRITE(4, int32_t, ((int32_t)RBUG_OP_PING));92WRITE(4, uint32_t, ((uint32_t)(__len / 4)));9394/* final pad */95PAD(__pos, 8);9697if (__pos != __len) {98__ret = -EINVAL;99} else {100rbug_connection_send_start(__con, RBUG_OP_PING, __len);101rbug_connection_write(__con, __data, __len);102__ret = rbug_connection_send_finish(__con, __serial);103}104105FREE(__data);106return __ret;107}108109int rbug_send_error(struct rbug_connection *__con,110uint32_t error,111uint32_t *__serial)112{113uint32_t __len = 0;114uint32_t __pos = 0;115uint8_t *__data = NULL;116int __ret = 0;117118LEN(8); /* header */119LEN(4); /* error */120121/* align */122PAD(__len, 8);123124__data = (uint8_t*)MALLOC(__len);125if (!__data)126return -ENOMEM;127128WRITE(4, int32_t, ((int32_t)RBUG_OP_ERROR));129WRITE(4, uint32_t, ((uint32_t)(__len / 4)));130WRITE(4, uint32_t, error); /* error */131132/* final pad */133PAD(__pos, 8);134135if (__pos != __len) {136__ret = -EINVAL;137} else {138rbug_connection_send_start(__con, RBUG_OP_ERROR, __len);139rbug_connection_write(__con, __data, __len);140__ret = rbug_connection_send_finish(__con, __serial);141}142143FREE(__data);144return __ret;145}146147int rbug_send_ping_reply(struct rbug_connection *__con,148uint32_t serial,149uint32_t *__serial)150{151uint32_t __len = 0;152uint32_t __pos = 0;153uint8_t *__data = NULL;154int __ret = 0;155156LEN(8); /* header */157LEN(4); /* serial */158159/* align */160PAD(__len, 8);161162__data = (uint8_t*)MALLOC(__len);163if (!__data)164return -ENOMEM;165166WRITE(4, int32_t, ((int32_t)RBUG_OP_PING_REPLY));167WRITE(4, uint32_t, ((uint32_t)(__len / 4)));168WRITE(4, uint32_t, serial); /* serial */169170/* final pad */171PAD(__pos, 8);172173if (__pos != __len) {174__ret = -EINVAL;175} else {176rbug_connection_send_start(__con, RBUG_OP_PING_REPLY, __len);177rbug_connection_write(__con, __data, __len);178__ret = rbug_connection_send_finish(__con, __serial);179}180181FREE(__data);182return __ret;183}184185int rbug_send_error_reply(struct rbug_connection *__con,186uint32_t serial,187uint32_t error,188uint32_t *__serial)189{190uint32_t __len = 0;191uint32_t __pos = 0;192uint8_t *__data = NULL;193int __ret = 0;194195LEN(8); /* header */196LEN(4); /* serial */197LEN(4); /* error */198199/* align */200PAD(__len, 8);201202__data = (uint8_t*)MALLOC(__len);203if (!__data)204return -ENOMEM;205206WRITE(4, int32_t, ((int32_t)RBUG_OP_ERROR_REPLY));207WRITE(4, uint32_t, ((uint32_t)(__len / 4)));208WRITE(4, uint32_t, serial); /* serial */209WRITE(4, uint32_t, error); /* error */210211/* final pad */212PAD(__pos, 8);213214if (__pos != __len) {215__ret = -EINVAL;216} else {217rbug_connection_send_start(__con, RBUG_OP_ERROR_REPLY, __len);218rbug_connection_write(__con, __data, __len);219__ret = rbug_connection_send_finish(__con, __serial);220}221222FREE(__data);223return __ret;224}225226struct rbug_proto_noop * rbug_demarshal_noop(struct rbug_proto_header *header)227{228struct rbug_proto_noop *ret;229230if (!header)231return NULL;232if (header->opcode != (int32_t)RBUG_OP_NOOP)233return NULL;234235ret = MALLOC(sizeof(*ret));236if (!ret)237return NULL;238239ret->header.__message = header;240ret->header.opcode = header->opcode;241242return ret;243}244245struct rbug_proto_ping * rbug_demarshal_ping(struct rbug_proto_header *header)246{247struct rbug_proto_ping *ret;248249if (!header)250return NULL;251if (header->opcode != (int32_t)RBUG_OP_PING)252return NULL;253254ret = MALLOC(sizeof(*ret));255if (!ret)256return NULL;257258ret->header.__message = header;259ret->header.opcode = header->opcode;260261return ret;262}263264struct rbug_proto_error * rbug_demarshal_error(struct rbug_proto_header *header)265{266uint32_t len = 0;267uint32_t pos = 0;268uint8_t *data = NULL;269struct rbug_proto_error *ret;270271if (!header)272return NULL;273if (header->opcode != (int32_t)RBUG_OP_ERROR)274return NULL;275276pos = 0;277len = header->length * 4;278data = (uint8_t*)&header[1];279ret = MALLOC(sizeof(*ret));280if (!ret)281return NULL;282283ret->header.__message = header;284ret->header.opcode = header->opcode;285286READ(4, uint32_t, error); /* error */287288return ret;289}290291struct rbug_proto_ping_reply * rbug_demarshal_ping_reply(struct rbug_proto_header *header)292{293uint32_t len = 0;294uint32_t pos = 0;295uint8_t *data = NULL;296struct rbug_proto_ping_reply *ret;297298if (!header)299return NULL;300if (header->opcode != (int32_t)RBUG_OP_PING_REPLY)301return NULL;302303pos = 0;304len = header->length * 4;305data = (uint8_t*)&header[1];306ret = MALLOC(sizeof(*ret));307if (!ret)308return NULL;309310ret->header.__message = header;311ret->header.opcode = header->opcode;312313READ(4, uint32_t, serial); /* serial */314315return ret;316}317318struct rbug_proto_error_reply * rbug_demarshal_error_reply(struct rbug_proto_header *header)319{320uint32_t len = 0;321uint32_t pos = 0;322uint8_t *data = NULL;323struct rbug_proto_error_reply *ret;324325if (!header)326return NULL;327if (header->opcode != (int32_t)RBUG_OP_ERROR_REPLY)328return NULL;329330pos = 0;331len = header->length * 4;332data = (uint8_t*)&header[1];333ret = MALLOC(sizeof(*ret));334if (!ret)335return NULL;336337ret->header.__message = header;338ret->header.opcode = header->opcode;339340READ(4, uint32_t, serial); /* serial */341READ(4, uint32_t, error); /* error */342343return ret;344}345346347