Ejemplo n.º 1
0
void * fifo_rx(void * parameters) {
    csp_packet_t *buf = csp_buffer_get(BUF_SIZE);
    /* Wait for packet on fifo */
    while (read(rx_channel, &buf->length, BUF_SIZE) > 0) {
        csp_new_packet(buf, &csp_if_fifo, NULL);
        buf = csp_buffer_get(BUF_SIZE);
    }

    return NULL;
}
Ejemplo n.º 2
0
int eps_slave_single_output(struct command_context *ctx) {

	char * args = command_args(ctx);
	unsigned int channel;
	unsigned int mode;
	int delay;
	printf("Input channel, mode (0=off, 1=on), and delay\r\n");
	if (sscanf(args, "%u %u %d", &channel, &mode, &delay) != 3)
		return CMD_ERROR_SYNTAX;
	printf("Channel %d is set to %d with delay %d\r\n", channel, mode, delay);

	eps_output_set_single_req eps_switch;
	eps_switch.channel = (uint8_t)channel;
	eps_switch.mode = (uint8_t)mode;
	eps_switch.delay = csp_hton16((int16_t)delay);

	i2c_frame_t * frame;
	frame = csp_buffer_get(I2C_MTU);
	frame->dest = slave_node;
	frame->data[0] = EPS_PORT_SET_SINGLE_OUTPUT; // Ping port
	memcpy(&frame->data[1], &eps_switch, sizeof(eps_switch));
	frame->len = 1 + sizeof(eps_switch);
	frame->len_rx = 0;
	frame->retries = 0;

	if (i2c_send(0, frame, 0) != E_NO_ERR) {
		csp_buffer_free(frame);
		return CMD_ERROR_NOMEM;
	}

	return CMD_ERROR_NONE;

}
Ejemplo n.º 3
0
int eps_slave_output(struct command_context *ctx) {

	char * args = command_args(ctx);
	unsigned int outputs;
	printf("console args: %s\r\n", args);
	if (sscanf(args, "%X", &outputs) != 1)
		return CMD_ERROR_SYNTAX;
	printf("Outputs 0x%02X\r\n", outputs);

	i2c_frame_t * frame;
	frame = csp_buffer_get(I2C_MTU);
	frame->dest = slave_node;
	frame->data[0] = EPS_PORT_SET_OUTPUT; // Ping port
	frame->data[1] = outputs;
	frame->len = 2;
	frame->len_rx = 0;
	frame->retries = 0;

	if (i2c_send(0, frame, 0) != E_NO_ERR) {
		csp_buffer_free(frame);
		return CMD_ERROR_NOMEM;
	}

	return CMD_ERROR_NONE;

}
Ejemplo n.º 4
0
int eps_slave_ppt_mode(struct command_context *ctx) {

	char * args = command_args(ctx);
	unsigned int mode;
	printf("EPS_PPT_MODE_OFF = 0, EPS_PPT_MODE_AUTO = 1, EPS_PPT_MODE_FIXED= 2\r\n");
	if (sscanf(args, "%d", &mode) != 1)
		return CMD_ERROR_SYNTAX;
	printf("Mode %d\r\n", mode);

	i2c_frame_t * frame;
	frame = csp_buffer_get(I2C_MTU);
	frame->dest = slave_node;
	frame->data[0] = EPS_PORT_SET_PPTMODE; // Ping port
	frame->data[1] = (uint8_t)mode;
	frame->len = 2;
	frame->len_rx = 0;
	frame->retries = 0;

	if (i2c_send(0, frame, 0) != E_NO_ERR) {
		csp_buffer_free(frame);
		return CMD_ERROR_NOMEM;
	}

	return CMD_ERROR_NONE;

}
Ejemplo n.º 5
0
void csp_ping_noreply(uint8_t node) {

	/* Prepare data */
	csp_packet_t * packet;
	packet = csp_buffer_get(1);

	/* Check malloc */
	if (packet == NULL)
		return;

	/* Open connection */
	csp_conn_t * conn = csp_connect(CSP_PRIO_NORM, node, CSP_PING, 0, 0);
	if (conn == NULL) {
		csp_buffer_free(packet);
		return;
	}

	packet->data[0] = 0x55;
	packet->length = 1;

	printf("Ping ignore reply node %u.\r\n", node);

	/* Try to send frame */
	if (!csp_send(conn, packet, 0))
		csp_buffer_free(packet);

	csp_close(conn);

}
Ejemplo n.º 6
0
int eps_slave_ping(struct command_context *ctx) {

	i2c_frame_t * frame;
	frame = csp_buffer_get(I2C_MTU);
	frame->dest = slave_node;
	frame->data[0] = CSP_PING; // Ping port
	frame->data[1] = 0x55;
	frame->len = 2;
	frame->len_rx = 3;
	frame->retries = 0;

	if (i2c_send(0, frame, 0) != E_NO_ERR) {
		csp_buffer_free(frame);
		return CMD_ERROR_NOMEM;
	}

	if (i2c_receive(0, &frame, 20) == E_NO_ERR) {
		printf("Received a reply from EPS!\r\n");
	} else {
		printf("No reply from EPS\r\n");
	}

	csp_buffer_free(frame);
	return CMD_ERROR_NONE;

}
Ejemplo n.º 7
0
int eps_slave_hk2(struct command_context *ctx) {

	printf("Requesting EPS HK2 data\r\n");
	eps_hk_t * chkparam;

	i2c_frame_t * frame;
	frame = csp_buffer_get(I2C_MTU);
	frame->dest = slave_node;
	frame->data[0] = EPS_PORT_HK;
	frame->data[1] = 0;
	frame->len = 2;
	frame->len_rx = 2 + (uint8_t) sizeof(eps_hk_t);
	frame->retries = 0;

	if (i2c_send(0, frame, 0) != E_NO_ERR) {
		csp_buffer_free(frame);
		return CMD_ERROR_NOMEM;
	}

	if (i2c_receive(0, &frame, 20) != E_NO_ERR)
		return CMD_ERROR_FAIL;

	chkparam = (eps_hk_t *)&frame->data[2];
	eps_hk_unpack(chkparam);
	eps_hk_print(chkparam);

	csp_buffer_free(frame);
	return CMD_ERROR_NONE;

}
Ejemplo n.º 8
0
int eps_slave_volt(struct command_context *ctx) {

	char * args = command_args(ctx);
	unsigned int pv1, pv2, pv3;
	if (sscanf(args, "%u %u %u", &pv1, &pv2, &pv3) != 3)
		return CMD_ERROR_SYNTAX;
	printf("PV1: %04d PV2: %04d PV3: %04d\r\n", pv1, pv2, pv3);

	uint16_t pvolt[3];
	pvolt[0] = csp_hton16(pv1);
	pvolt[1] = csp_hton16(pv2);
	pvolt[2] = csp_hton16(pv3);

	i2c_frame_t * frame;
	frame = csp_buffer_get(I2C_MTU);
	frame->dest = slave_node;
	frame->data[0] = EPS_PORT_SET_VBOOST; // Ping port
	memcpy(&frame->data[1], &pvolt, 3 * sizeof(uint16_t));
	frame->len = 1 + 3 * sizeof(uint16_t);
	frame->len_rx = 0;
	frame->retries = 0;

	if (i2c_send(0, frame, 0) != E_NO_ERR) {
		csp_buffer_free(frame);
		return CMD_ERROR_NOMEM;
	}

	return CMD_ERROR_NONE;

}
Ejemplo n.º 9
0
void * _csp_realloc(void * buff, size_t old_size, size_t new_size)
{
    void * _data = csp_buffer_get(new_size);
    if (NULL == _data)
        return NULL;
    memcpy(_data, buff, old_size);
    return _data;
}
Ejemplo n.º 10
0
int csp_ping(uint8_t node, uint32_t timeout, unsigned int size, uint8_t conn_options) {

	int i;
	uint32_t start, time, status = 0;

	/* Counter */
	start = csp_get_ms();

	/* Open connection */
	csp_conn_t * conn = csp_connect(CSP_PRIO_NORM, node, CSP_PING, timeout, conn_options);
	if (conn == NULL)
		return -1;

	/* Prepare data */
	csp_packet_t * packet;
	packet = csp_buffer_get(size);
	if (packet == NULL)
		goto out;

	/* Set data to increasing numbers */
	packet->length = size;
	for (i = 0; i < size; i++)
		packet->data[i] = i;

	/* Try to send frame */
	if (!csp_send(conn, packet, 0))
		goto out;

	/* Read incoming frame */
	packet = csp_read(conn, timeout);
	if (packet == NULL)
		goto out;

	/* Ensure that the data was actually echoed */
	for (i = 0; i < size; i++)
		if (packet->data[i] != i)
			goto out;

	status = 1;

out:
	/* Clean up */
	if (packet != NULL)
		csp_buffer_free(packet);
	csp_close(conn);

	/* We have a reply */
	time = (csp_get_ms() - start);

	if (status) {
		return time;
	} else {
		return -1;
	}

}
Ejemplo n.º 11
0
int send_test_packet(void) {

	csp_packet_t * packet = csp_buffer_get(100);
	char * str = "Hello World\r\n";
	sprintf((char *) packet->data, str);
	packet->length = strlen(str) + 1;
	if (csp_sendto(CSP_PRIO_NORM, NODE_OBC, OBC_PORT_CADCS_HK, 0, CSP_O_CRC32 | CSP_O_HMAC | CSP_O_XTEA, packet, 0) < 0) {
		//printf("Error sending packet\r\n");
		csp_buffer_free(packet);
	}

	return 0;

}
Ejemplo n.º 12
0
unsigned WINAPI fifo_rx(void *handle) {
    printf("fifo_rx tid: %lu\n", GetCurrentThreadId());
    HANDLE pipe = (HANDLE) handle;
    csp_packet_t *buf = csp_buffer_get(BUF_SIZE);
    DWORD bytesRead;
    BOOL readSuccess;

    while(1) {
        readSuccess = 
            ReadFile(pipe, &buf->length, BUF_SIZE, &bytesRead, NULL);
        if( !readSuccess || bytesRead == 0 ) {
            csp_buffer_free(buf);
            printError();
            break;
        }
        csp_new_packet(buf, &csp_if_fifo, NULL);
        buf = csp_buffer_get(BUF_SIZE);
    }
    printf("Closing pipe to client\n");
    CloseHandle(pipe);

    return 0;
}
Ejemplo n.º 13
0
void *csp_buffer_clone(void *buffer) {

	csp_packet_t *packet = (csp_packet_t *) buffer;

	if (!packet)
		return NULL;

	csp_packet_t *clone = csp_buffer_get(packet->length);

	if (clone)
		memcpy(clone, packet, size);

	return clone;

}
Ejemplo n.º 14
0
int eps_slave_hardreset(struct command_context *ctx) {


	i2c_frame_t * frame;
	frame = csp_buffer_get(I2C_MTU);
	frame->dest = slave_node;
	frame->data[0] = EPS_PORT_HARDRESET;
	frame->len = 1;
	frame->len_rx = 0;
	frame->retries = 0;

	if (i2c_send(0, frame, 0) != E_NO_ERR) {
		csp_buffer_free(frame);
		return CMD_ERROR_NOMEM;
	}
	return CMD_ERROR_NONE;
}
Ejemplo n.º 15
0
void csp_tiradio_rx (csp_iface_t *interface,
                      uint8_t *buf, int len, void *xTaskWoken) {
    csp_packet_t *packet;

    csp_tiradio_driver_handle_t* handle =
        (csp_tiradio_driver_handle_t*) (interface->driver);

    if (len < CSP_HEADER_LENGTH) {
        csp_log_warn("Length less than minimum expected! Size %u,"
                     " expected %u; dropping message\r\n", len,
                     CSP_HEADER_LENGTH);
        return;
    }

    packet = csp_buffer_get(interface->mtu);

    if (packet != NULL) {

        memcpy(&packet->id.ext, buf, len);

        packet->length = len;

        if (packet->length >= CSP_HEADER_LENGTH &&
            packet->length <= interface->mtu + CSP_HEADER_LENGTH) {

            /* Strip CSP header off the length field*/
            packet->length -= CSP_HEADER_LENGTH;

            /* Convert the packet from network to host order */
            packet->id.ext = csp_ntoh32(packet->id.ext);

            csp_new_packet(packet, interface, xTaskWoken);

            if (handle->module_id < NUM_TIRADIO_MODULES)
                latest_csp_transfer_id[handle->module_id] = packet->id;
        }
        else {
            interface->frame++;
            csp_buffer_free(packet);
        }
    }
    else {
        interface->frame++;
    }
}
Ejemplo n.º 16
0
Archivo: pca9665.c Proyecto: lirihe/arm
/**
 * Context: Task only
 */
int i2c_master_transaction(int handle, uint8_t addr, void * txbuf, size_t txlen, void * rxbuf, size_t rxlen, uint16_t timeout) {

	if (handle >= pca9665_device_count)
		return E_NO_DEVICE;

	if (!device[handle].is_initialised)
		return E_NO_DEVICE;

	if ((txlen > I2C_MTU - 10) || (rxlen > I2C_MTU - 10))
		return E_INVALID_BUF_SIZE;

	i2c_frame_t * frame = csp_buffer_get(I2C_MTU);
	if (frame == NULL)
		return E_NO_BUFFER;

	xSemaphoreTake(i2c_lock, 10 * configTICK_RATE_HZ);

	frame->dest = addr;
	memcpy(&frame->data[0], txbuf, txlen);
	frame->len = txlen;
	frame->len_rx = rxlen;

	if (i2c_send(handle, frame, 0) != E_NO_ERR) {
		csp_buffer_free(frame);
		xSemaphoreGive(i2c_lock);
		return E_TIMEOUT;
	}

	if (rxlen == 0) {
		xSemaphoreGive(i2c_lock);
		return E_NO_ERR;
	}

	if (i2c_receive(handle, &frame, timeout) != E_NO_ERR) {
		xSemaphoreGive(i2c_lock);
		return E_TIMEOUT;
	}

	memcpy(rxbuf, &frame->data[0], rxlen);

	csp_buffer_free(frame);
	xSemaphoreGive(i2c_lock);
	return E_NO_ERR;

}
Ejemplo n.º 17
0
void gps_rx_cb (void * arg, uint8_t *buf, int len, void *pxTaskWoken)
{
    gps_cfg_t *gps_cfg = (gps_cfg_t *) arg;
    DEBUG("GPS_RX: %c\n", *buf);

    if (*buf == '\n' || *buf == '\r') {
        gps_fix_t *fix = gps_incr_fix();
        int len = gps_buf_cur + 1;
        int result = nmea_parse(gps_buf, len, fix);
        memset(gps_buf, '\0', len);
        gps_buf_cur = 0;

        if (result != NMEA_OK) {
            DEBUG("error parsing NMEA: %d\n", result);
            return;
        }

        // Send location fix
        if (NULL != gps_cfg->conn) {
            DEBUG("Parsed NMEA sentence, sending message\n");
            DEBUG("NMEA %02d:%02d:%02d.%03d\n", fix->hour, fix->minute,
                    fix->seconds, fix->milliseconds);

            csp_packet_t * msg;
            msg = csp_buffer_get(sizeof(gps_fix_t));
            memcpy(msg->data, fix, sizeof(gps_fix_t));
            //*((gps_fix_t*)msg->data) = *fix;
            msg->length = sizeof(gps_fix_t);

            if (!csp_send(gps_cfg->conn, msg, 1000)) {
                DEBUG("csp_send failed\n");
                csp_buffer_free(msg);
            }
        }
        return;
    }

    gps_buf[gps_buf_cur++] = *buf;
    DEBUG("GPS_BUF: %.*s\n", gps_buf_cur, gps_buf);

    if (gps_buf_cur > GPS_BUFSIZE - 1) {
        DEBUG("Too much data for GPS buffer, discarding");
        gps_buf_cur = 0;
    }
}
Ejemplo n.º 18
0
void csp_promisc_add(csp_packet_t * packet, csp_queue_handle_t queue) {

    if (csp_promisc_enabled == 0)
        return;

    if (queue != NULL) {
        /* Make a copy of the message and queue it to the promiscuous task */
        csp_packet_t * packet_copy = csp_buffer_get(packet->length);
        if (packet_copy != NULL) {
            memcpy(&packet_copy->length, &packet->length, packet->length + 6);
            if (csp_queue_enqueue(queue, &packet_copy, 0) != CSP_QUEUE_OK) {
                csp_log_error("Promiscuous mode input queue full\r\n");
                csp_buffer_free(packet_copy);
            }
        }
    }

}
Ejemplo n.º 19
0
int csp_sfp_send(csp_conn_t * conn, void * data, int totalsize, int mtu, uint32_t timeout) {

	int count = 0;
	while(count < totalsize) {

		/* Allocate packet */
		csp_packet_t * packet = csp_buffer_get(mtu);
		if (packet == NULL)
			return -1;

		/* Calculate sending size */
		int size = totalsize - count;
		if (size > mtu)
			size = mtu;

		/* Print debug */
		csp_debug(CSP_PROTOCOL, "Sending SFP at %x size %u\r\n", data + count, size);

		/* Copy data */
		memcpy(packet->data, data + count, size);
		packet->length = size;

		/* Set fragment flag */
		conn->idout.flags |= CSP_FFRAG;

		/* Add SFP header */
		sfp_header_t * sfp_header = csp_sfp_header_add(packet);
		sfp_header->totalsize = csp_hton32(totalsize);
		sfp_header->offset = csp_hton32(count);

		/* Send data */
		if (!csp_send(conn, packet, timeout)) {
			csp_buffer_free(packet);
			return -1;
		}

		/* Increment count */
		count += size;

	}

	return 0;

}
Ejemplo n.º 20
0
/**
 * Use an existing connection to perform a transaction,
 * This is only possible if the next packet is on the same port and destination!
 * @param conn pointer to connection structure
 * @param timeout timeout in ms
 * @param outbuf pointer to outgoing data buffer
 * @param outlen length of request to send
 * @param inbuf pointer to incoming data buffer
 * @param inlen length of expected reply, -1 for unknown size (note inbuf MUST be large enough)
 * @return
 */
int csp_transaction_persistent(csp_conn_t * conn, unsigned int timeout, void * outbuf, int outlen, void * inbuf, int inlen) {

	/* Stupid way to implement max() but more portable than macros */
	int size = outlen;
	if (inlen > outlen)
		size = inlen;

	csp_packet_t * packet = csp_buffer_get(size);
	if (packet == NULL)
		return 0;

	/* Copy the request */
	if (outlen > 0 && outbuf != NULL)
		memcpy(packet->data, outbuf, outlen);
	packet->length = outlen;

	if (!csp_send(conn, packet, timeout)) {
		printf("Send failed\r\n");
		csp_buffer_free(packet);
		return 0;
	}

	/* If no reply is expected, return now */
	if (inlen == 0)
		return 1;

	packet = csp_read(conn, timeout);
	if (packet == NULL) {
		printf("Read failed\r\n");
		return 0;
	}

	if ((inlen != -1) && (packet->length != inlen)) {
		printf("Reply length %u expected %u\r\n", packet->length, inlen);
		csp_buffer_free(packet);
		return 0;
	}

	memcpy(inbuf, packet->data, packet->length);
	int length = packet->length;
	csp_buffer_free(packet);
	return length;

}
Ejemplo n.º 21
0
int cmd_eps_slave_config_restore(struct command_context *ctx) {


	i2c_frame_t * frame;
	frame = csp_buffer_get(I2C_MTU);
	frame->dest = slave_node;
	frame->data[0] = EPS_PORT_CONFIG_CMD;
	frame->data[1] = 1;
	frame->len = 2;
	frame->len_rx = 0;
	frame->retries = 0;

	if (i2c_send(0, frame, 0) != E_NO_ERR) {
		csp_buffer_free(frame);
		return CMD_ERROR_NOMEM;
	}

	return CMD_ERROR_NONE;
}
Ejemplo n.º 22
0
void csp_ps(uint8_t node, uint32_t timeout) {

	/* Open connection */
	csp_conn_t * conn = csp_connect(CSP_PRIO_NORM, node, CSP_PS, 0, 0);
	if (conn == NULL)
		return;

	/* Prepare data */
	csp_packet_t * packet;
	packet = csp_buffer_get(95);

	/* Check malloc */
	if (packet == NULL)
		goto out;

	packet->data[0] = 0x55;
	packet->length = 1;

	printf("PS node %u: ", node);

	/* Try to send frame */
	if (!csp_send(conn, packet, 0))
		goto out;

	/* Read incoming frame */
	packet = csp_read(conn, timeout);
	if (packet == NULL) {
		printf(" Timeout!\r\n");
		goto out;
	}

	/* We have a reply */
	printf("PS Length %u\r\n", packet->length);
	printf("%s\r\n", packet->data);

	/* Clean up */
out:
	if (packet != NULL)
		csp_buffer_free(packet);
	csp_close(conn);

}
Ejemplo n.º 23
0
int eps_slave_reset_persistent(struct command_context *ctx) {

	uint8_t magic = 0x42;

	i2c_frame_t * frame;
	frame = csp_buffer_get(I2C_MTU);
	frame->dest = slave_node;
	frame->data[0] = EPS_PORT_RESET_COUNTERS;
	frame->data[1] = (uint8_t) magic;
	frame->len = 2;
	frame->len_rx = 0;
	frame->retries = 0;

	if (i2c_send(0, frame, 0) != E_NO_ERR) {
		csp_buffer_free(frame);
		return CMD_ERROR_NOMEM;
	}

	return CMD_ERROR_NONE;

}
Ejemplo n.º 24
0
int csp_transaction_persistent(csp_conn_t * conn, uint32_t timeout, void * outbuf, int outlen, void * inbuf, int inlen) {

	int size = (inlen > outlen) ? inlen : outlen;
	csp_packet_t * packet = csp_buffer_get(size);
	if (packet == NULL)
		return 0;

	/* Copy the request */
	if (outlen > 0 && outbuf != NULL)
		memcpy(packet->data, outbuf, outlen);
	packet->length = outlen;

	if (!csp_send(conn, packet, timeout)) {
		csp_buffer_free(packet);
		return 0;
	}

	/* If no reply is expected, return now */
	if (inlen == 0)
        {
		return 1;
        }

	packet = csp_read(conn, timeout);
	if (packet == NULL)
		return 0;

	if ((inlen != -1) && ((int)packet->length != inlen)) {
		csp_log_error("Reply length %u expected %u\r\n", packet->length, inlen);
		csp_buffer_free(packet);
		return 0;
	}

	memcpy(inbuf, packet->data, packet->length);
	int length = packet->length;
	csp_buffer_free(packet);
	return length;

}
Ejemplo n.º 25
0
int cmd_eps_slave_config_set(struct command_context *ctx) {
	printf("Setting EPS Config\r\n");

	i2c_frame_t * frame;
	frame = csp_buffer_get(I2C_MTU);
	frame->dest = slave_node;
	frame->data[0] = EPS_PORT_CONFIG_SET;
	frame->len = 1 + (uint8_t) sizeof(eps_config_t);
	frame->len_rx = 2;
	frame->retries = 0;

	memcpy(&frame->data[1], &nanopower_config, sizeof(eps_config_t));
	eps_config_pack((eps_config_t *) &frame->data[1]);
	if (i2c_send(0, frame, 0) != E_NO_ERR) {
		csp_buffer_free(frame);
		return CMD_ERROR_NOMEM;
	}

	if (i2c_receive(0, &frame, 200) != E_NO_ERR)
		return CMD_ERROR_FAIL;

	return CMD_ERROR_NONE;
}
Ejemplo n.º 26
0
int eps_slave_reboot(struct command_context *ctx) {

	i2c_frame_t * frame;
	frame = csp_buffer_get(I2C_MTU);
	frame->dest = slave_node;
	frame->data[0] = CSP_REBOOT; // Ping port
	frame->data[1] = 0x80;
	frame->data[2] = 0x07;
	frame->data[3] = 0x80;
	frame->data[4] = 0x07;
	frame->len = 5;
	frame->len_rx = 0;
	frame->retries = 0;

	if (i2c_send(0, frame, 0) != E_NO_ERR) {
		csp_buffer_free(frame);
		return CMD_ERROR_NOMEM;
	}

	printf("Sent reboot command to EPS\r\n");
	return CMD_ERROR_NONE;

}
Ejemplo n.º 27
0
int sendpacket() {
	csp_packet_t *packet;
	csp_conn_t *conn;
	packet = csp_buffer_get(10);
	if (packet == NULL) {
		printf("failed to get packet in sendpacket()\n");
		return CSP_TASK_RETURN;
	}
	conn = csp_connect(CSP_PRIO_NORM, MY_ADDRESS, MY_PORT, 1000, CSP_O_NONE);
	if (conn == NULL) {
		printf("Connection failed in sendpacket()\n");
		return CSP_TASK_RETURN;
	}
	char *msg = "42!";
	strcpy((char *) packet->data, msg);
	packet->length = strlen(msg);
	if (!csp_send(conn, packet, 1000)) {
		printf("Sending packet from sendpacket() failed\n");
		csp_buffer_free(packet);
	}
	csp_close(conn);
	return;
}
Ejemplo n.º 28
0
unsigned WINAPI task_client(void * parameters) {

	csp_packet_t * packet;
	csp_conn_t * conn;

	while (1) {

		/**
		 * Try ping
		 */

		Sleep(1000);

		int result = csp_ping(MY_ADDRESS, 100, 100, CSP_O_NONE);
		printf("Ping result %d [ms]\r\n", result);

		Sleep(1000);

		/**
		 * Try data packet to server
		 */

		/* Get packet buffer for data */
		packet = csp_buffer_get(100);
		if (packet == NULL) {
			/* Could not get buffer element */
			printf("Failed to get buffer element\n");
			return 0;
		}

		/* Connect to host HOST, port PORT with regular UDP-like protocol and 1000 ms timeout */
		conn = csp_connect(CSP_PRIO_NORM, MY_ADDRESS, MY_PORT, 1000, CSP_O_NONE);
		if (conn == NULL) {
			/* Connect failed */
			printf("Connection failed\n");
			/* Remember to free packet buffer */
			csp_buffer_free(packet);
			return 0;
		}

		/* Copy dummy data to packet */
		char *msg = "Hello World";
		strcpy((char *) packet->data, msg);

		/* Set packet length */
		packet->length = strlen(msg);

		/* Send packet */
		if (!csp_send(conn, packet, 1000)) {
			/* Send failed */
			printf("Send failed\n");
			csp_buffer_free(packet);
		}

		/* Close connection */
		csp_close(conn);

	}

	return 0;
}
Ejemplo n.º 29
0
void csp_service_handler(csp_conn_t * conn, csp_packet_t * packet) {

	switch (csp_conn_dport(conn)) {

	case CSP_CMP:
		/* Pass to CMP handler */
		if (csp_cmp_handler(conn, packet) != CSP_ERR_NONE) {
			csp_buffer_free(packet);
			return;
		}
		break;

	case CSP_PING:
		/* A ping means, just echo the packet, so no changes */
		csp_log_info("SERVICE: Ping received");
		break;

	case CSP_PS: {
		/* Sanity check on request */
		if ((packet->length != 1) || (packet->data[0] != 0x55)) {
			/* Sanity check failed */
			csp_buffer_free(packet);
			/* Clear the packet, it has been freed */
			packet = NULL;
			break;
		}
		/* Start by allocating just the right amount of memory */
		int task_list_size = csp_sys_tasklist_size();
		char * pslist = csp_malloc(task_list_size);
		/* Check for malloc fail */
		if (pslist == NULL) {
			/* Send out the data */
			strcpy((char *)packet->data, "Not enough memory");
			packet->length = strlen((char *)packet->data);
			/* Break and let the default handling send packet */
			break;
		}

		/* Retrieve the tasklist */
		csp_sys_tasklist(pslist);
		int pslen = strnlen(pslist, task_list_size);

		/* Split the potentially very long string into packets */
		int i = 0;
		while(i < pslen) {

			/* Allocate packet buffer, if need be */
			if (packet == NULL)
				packet = csp_buffer_get(CSP_RPS_MTU);
			if (packet == NULL)
				break;

			/* Calculate length, either full MTU or the remainder */
			packet->length = (pslen - i > CSP_RPS_MTU) ? CSP_RPS_MTU : (pslen - i);

			/* Send out the data */
			memcpy(packet->data, &pslist[i], packet->length);
			i += packet->length;
			if (!csp_send(conn, packet, 0))
				csp_buffer_free(packet);

			/* Clear the packet reference when sent */
			packet = NULL;

		}
		csp_free(pslist);
		break;
	}

	case CSP_MEMFREE: {
		uint32_t total = csp_sys_memfree();

		total = csp_hton32(total);
		memcpy(packet->data, &total, sizeof(total));
		packet->length = sizeof(total);

		break;
	}

	case CSP_REBOOT: {
		uint32_t magic_word;
		memcpy(&magic_word, packet->data, sizeof(magic_word));

		magic_word = csp_ntoh32(magic_word);

		/* If the magic word is valid, reboot */
		if (magic_word == CSP_REBOOT_MAGIC) {
			csp_sys_reboot();
		} else if (magic_word == CSP_REBOOT_SHUTDOWN_MAGIC) {
			csp_sys_shutdown();
		}


		
		csp_buffer_free(packet);
		return;
	}

	case CSP_BUF_FREE: {
		uint32_t size = csp_buffer_remaining();
		size = csp_hton32(size);
		memcpy(packet->data, &size, sizeof(size));
		packet->length = sizeof(size);
		break;
	}

	case CSP_UPTIME: {
		uint32_t time = csp_get_s();
		time = csp_hton32(time);
		memcpy(packet->data, &time, sizeof(time));
		packet->length = sizeof(time);
		break;
	}

	default:
		csp_buffer_free(packet);
		return;
	}

	if (packet != NULL) {
		if (!csp_send(conn, packet, 0))
			csp_buffer_free(packet);
	}

}
Ejemplo n.º 30
0
int eps_slave_hk(struct command_context *ctx) {
	int bintmp;
	printf("Requesting EPS HK data\r\n");
	eps_hk_1_t * chkparam;

	i2c_frame_t * frame;
	frame = csp_buffer_get(I2C_MTU);
	frame->dest = slave_node;
	frame->data[0] = EPS_PORT_HK; // Ping port
	frame->len = 1;
	frame->len_rx = 2 + (uint8_t) sizeof(eps_hk_1_t);
	frame->retries = 0;

	if (i2c_send(0, frame, 0) != E_NO_ERR) {
		csp_buffer_free(frame);
		return CMD_ERROR_NOMEM;
	}

	if (i2c_receive(0, &frame, 20) != E_NO_ERR)
		return CMD_ERROR_FAIL;

	chkparam = (eps_hk_1_t *)&frame->data[2];

	chkparam->pv[0] = csp_ntoh16(chkparam->pv[0]);
	chkparam->pv[1] = csp_ntoh16(chkparam->pv[1]);
	chkparam->pv[2] = csp_ntoh16(chkparam->pv[2]);
	chkparam->pc = csp_ntoh16(chkparam->pc);
	chkparam->bv = csp_ntoh16(chkparam->bv);
	chkparam->sc = csp_ntoh16(chkparam->sc);
	chkparam->temp[0] = csp_ntoh16(chkparam->temp[0]);
	chkparam->temp[1] = csp_ntoh16(chkparam->temp[1]);
	chkparam->temp[2] = csp_ntoh16(chkparam->temp[2]);
	chkparam->temp[3] = csp_ntoh16(chkparam->temp[3]);
	chkparam->temp[4] = csp_ntoh16(chkparam->temp[4]);
	chkparam->temp[5] = csp_ntoh16(chkparam->temp[5]);
	chkparam->latchup[0] = csp_ntoh16(chkparam->latchup[0]);
	chkparam->latchup[1] = csp_ntoh16(chkparam->latchup[1]);
	chkparam->latchup[2] = csp_ntoh16(chkparam->latchup[2]);
	chkparam->latchup[3] = csp_ntoh16(chkparam->latchup[3]);
	chkparam->latchup[4] = csp_ntoh16(chkparam->latchup[4]);
	chkparam->latchup[5] = csp_ntoh16(chkparam->latchup[5]);
	chkparam->sw_errors = csp_ntoh16(chkparam->sw_errors);
	chkparam->bootcount = csp_ntoh16(chkparam->bootcount);

	printf("ADC sample:\r\n");
	printf("Temp 1 %"PRId16"\r\n", chkparam->temp[0]);
	printf("Temp 2 %"PRId16"\r\n", chkparam->temp[1]);
	printf("Temp 3 %"PRId16"\r\n", chkparam->temp[2]);
	printf("Temp PCB %"PRId16"\r\n", chkparam->temp[3]);
	printf("BV %u\r\n", chkparam->bv);
	printf("PC %u\r\n", chkparam->pc);
	printf("SC %u\r\n", chkparam->sc);
	printf("PV1 %u\r\n", chkparam->pv[0]);
	printf("PV2 %u\r\n", chkparam->pv[1]);
	printf("PV3 %u\r\n", chkparam->pv[2]);
	printf("Latch up 1 %u\r\n", chkparam->latchup[0]);
	printf("Latch up 2 %u\r\n", chkparam->latchup[1]);
	printf("Latch up 3 %u\r\n", chkparam->latchup[2]);
	printf("Latch up 4 %u\r\n", chkparam->latchup[3]);
	printf("Latch up 5 %u\r\n", chkparam->latchup[4]);
	printf("Latch up 6 %u\r\n", chkparam->latchup[5]);
	printf("User Channel Status %02X ; \r\n", chkparam->channel_status);
	bintmp = chkparam->channel_status;
	int n;
	for(n=0; n<8; n++)
	   {
	      if((bintmp & 0x80) !=0)
	      {
	         printf("1");
	      }
	      else
	      {
	         printf("0");
	      }
	      if (n==3)
	      {
	         printf(" "); /* insert a space between nybbles */
	      }
	      bintmp = bintmp<<1;
	   }
	printf("\r\n");
	printf("Battery temperature 1 %"PRId16"\r\n", chkparam->temp[4]);
	printf("Battery temperature 2 %"PRId16"\r\n", chkparam->temp[5]);
	printf("Reset %d   BootCount %d   SW Err %d   PPT mode %d\r\n",chkparam->reset,chkparam->bootcount,chkparam->sw_errors,chkparam->ppt_mode);

	csp_buffer_free(frame);
	return CMD_ERROR_NONE;

}