<commit_before>#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/select.h>
#include <unistd.h>

#include <mqtt3.h>

typedef enum {
	stStart,
	stSocketOpened,
	stConnSent,
	stConnAckd,
	stSubSent,
	stSubAckd,
	stPause
} stateType;

static stateType state = stStart;

void mqtt_handle_publish(int sock, uint8_t header)
{
	uint8_t *topic, *payload;
	uint32_t remaining_length;
	uint8_t dup, qos, retain;

	dup = (header & 0x08)>>3;
	qos = (header & 0x06)>>1;
	retain = (header & 0x01);

	remaining_length = mqtt_read_remaining_length(sock);

	topic = mqtt_read_string(sock);
	remaining_length -= strlen((char *)topic) - 2;
	printf("Topic: %s\n", topic);
	free(topic);

	payload = malloc(remaining_length*sizeof(uint8_t));
	mqtt_read_bytes(sock, payload, remaining_length);
	printf("Payload: %s\n", payload);
	free(payload);
}

int handle_read(int sock)
{
	uint8_t buf;
	int rc;

	rc = read(sock, &buf, 1);
	printf("rc: %d\n", rc);
	if(rc == -1){
		printf("Error: %s\n", strerror(errno));
		return 1;
	}else if(rc == 0){
		return 2;
	}

	switch(buf&0xF0){
		case CONNACK:
			printf("Received CONNACK\n");
			read(sock, &buf, 1); // Remaining length
			printf("%d ", buf);
			read(sock, &buf, 1);//Reserved
			printf("%d ", buf);
			read(sock, &buf, 1); // Return code
			printf("%d\n", buf);
			state = stConnAckd;
			break;
		case SUBACK:
			printf("Received SUBACK\n");
			read(sock, &buf, 1); // Remaining length
			printf("%d ", buf);
			read(sock, &buf, 1); // Message ID MSB
			printf("%d ", buf);
			read(sock, &buf, 1); // Message ID LSB
			printf("%d ", buf);
			read(sock, &buf, 1); // Granted QoS
			printf("%d\n", buf);
			state = stSubAckd;
			break;
		case PINGREQ:
			printf("Received PINGREQ\n");
			mqtt_raw_pingresp(sock);
			break;
		case PINGRESP:
			printf("Received PINGRESP\n");
			//FIXME - do something!
			break;
		case PUBLISH:
			printf("Received PUBLISH\n");<commit_msg>Read whole fixed packet on receiving a PINGREQ/PINGRESP.<commit_after>#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/select.h>
#include <unistd.h>

#include <mqtt3.h>

typedef enum {
	stStart,
	stSocketOpened,
	stConnSent,
	stConnAckd,
	stSubSent,
	stSubAckd,
	stPause
} stateType;

static stateType state = stStart;

void mqtt_handle_publish(int sock, uint8_t header)
{
	uint8_t *topic, *payload;
	uint32_t remaining_length;
	uint8_t dup, qos, retain;

	dup = (header & 0x08)>>3;
	qos = (header & 0x06)>>1;
	retain = (header & 0x01);

	remaining_length = mqtt_read_remaining_length(sock);

	topic = mqtt_read_string(sock);
	remaining_length -= strlen((char *)topic) - 2;
	printf("Topic: %s\n", topic);
	free(topic);

	payload = malloc(remaining_length*sizeof(uint8_t));
	mqtt_read_bytes(sock, payload, remaining_length);
	printf("Payload: %s\n", payload);
	free(payload);
}

int handle_read(int sock)
{
	uint8_t buf;
	int rc;

	rc = read(sock, &buf, 1);
	printf("rc: %d\n", rc);
	if(rc == -1){
		printf("Error: %s\n", strerror(errno));
		return 1;
	}else if(rc == 0){
		return 2;
	}

	switch(buf&0xF0){
		case CONNACK:
			printf("Received CONNACK\n");
			read(sock, &buf, 1); // Remaining length
			printf("%d ", buf);
			read(sock, &buf, 1);//Reserved
			printf("%d ", buf);
			read(sock, &buf, 1); // Return code
			printf("%d\n", buf);
			state = stConnAckd;
			break;
		case SUBACK:
			printf("Received SUBACK\n");
			read(sock, &buf, 1); // Remaining length
			printf("%d ", buf);
			read(sock, &buf, 1); // Message ID MSB
			printf("%d ", buf);
			read(sock, &buf, 1); // Message ID LSB
			printf("%d ", buf);
			read(sock, &buf, 1); // Granted QoS
			printf("%d\n", buf);
			state = stSubAckd;
			break;
		case PINGREQ:
			printf("Received PINGREQ\n");
			mqtt_read_remaining_length(sock);
			mqtt_raw_pingresp(sock);
			break;
		case PINGRESP:
			printf("Received PINGRESP\n");
			mqtt_read_remaining_length(sock);
			//FIXME - do something!
			break;
		case PUBLISH:
			printf("Received PUBLISH\n");
{'fraction_non_alphanumeric': 0.1714008254430687, 'fraction_numerical': 0.016508861374119933, 'mean_word_length': 4.024390243902439, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7169637', 'n_tokens_mistral': 1855, 'n_tokens_neox': 1655, 'n_words': 380}