static void write_value_ota_usage(void)
{
printf("Usage: write-value-ota [options] <value_handle> \n"
"Options:\n"
"\t-w, --without-response\tWrite without response\n"
"\t-s, --signed-write\tSigned write command\n"
"e.g.:\n"
"\twrite-value-ota 0x0001 00 01 00\n");
}
static struct option write_value_ota_options[] = {
{"without-response", 0, 0, 'w'},
{"signed-write", 0, 0, 's'},
{}};
static void write_cb_ota(bool success, uint8_t att_ecode, void *user_data)
{
if (success)
{
PRLOG("\nWrite Ota successful\n");
}
else
{
PRLOG("\nWrite Ota failed: %s (0x%02x)\n", ecode_to_string(att_ecode), att_ecode);
}
}
static void cmd_write_value_ota(struct client *cli, char *cmd_str)
{
int opt, i, val;
char *argvbuf[516];
char **argv = argvbuf;
int argc = 1;
uint16_t handle;
char *endptr = NULL;
int length;
uint8_t *value = NULL;
bool without_response = false;
bool signed_write = false;
if (!bt_gatt_client_is_ready(cli->gatt))
{
printf("GATT client not initialized\n");
return;
}
if (!parse_args(cmd_str, 514, argv + 1, &argc))
{
printf("Too many arguments\n");
write_value_ota_usage();
return;
}
optind = 0;
argv[0] = "write-value-ota";
while ((opt = getopt_long(argc, argv, "+ws", write_value_ota_options, NULL)) != -1)
{
switch (opt)
{
case 'w':
without_response = true;
break;
case 's':
signed_write = true;
break;
default:
write_value_ota_usage();
return;
}
}
argc -= optind;
argv += optind;
if (argc < 1)
{
write_value_ota_usage();
return;
}
handle = strtol(argv[0], &endptr, 0);
if (!endptr || *endptr != '\0' || !handle)
{
printf("Invalid handle: %s\n", argv[0]);
return;
}
char *filename = "/home/aravindan/sample_bin.txt";
// Open file for reading
FILE *file = fopen(filename, "rb");
if (file == NULL)
{
fprintf(stderr, "Error opening file for reading.\n");
exit(1);
}
#define BUFFER_SIZE 4096
// Read file in chunks and send over socket
char buffer[BUFFER_SIZE];
size_t total_bytes_sent = 0;
int count = 0;
struct stat st;
if (stat(filename, &st) == 0)
{
printf("Size of file: %ld bytes.\n", st.st_size);
}
else
{
printf("Unable to get file size.\n");
}
int size = st.st_size;
length = size;
printf("length of the text:%d\n", length);
if (length > 0)
{
if (length > UINT16_MAX)
{
printf("Write value too long\n");
return;
}
value = malloc(length);
if (!value)
{
printf("Failed to construct write value\n");
return;
}
while (1)
{
size_t bytes_read = fread(buffer, 1, BUFFER_SIZE, file);
if (bytes_read == 0)
{
// End of file
break;
}
printf("value [%d]\n", value[i - 1]);
for (i = 1; i < size; i++)
{
count++;
value[i - 1] = (uint8_t)buffer[i - 1];
printf("value [%d] :: count [%d] \n", value[i - 1], count);
}
}
}
if (without_response)
{
if (!bt_gatt_client_write_without_response(cli->gatt, handle, signed_write, value, length))
{
printf("Failed to initiate write without response "
"procedure\n");
goto done;
}
printf("Write command sent\n");
goto done;
}
for (i = 0; i < size; i += BUFFER_SIZE)
{
if (!bt_gatt_client_write_value(cli->gatt, handle, value, length, write_cb_ota, NULL, NULL))
{
printf("Failed to initiate write procedure\n");
}
else
{
printf("initiated write procedure\n");
usleep(10000); // add a small delay to avoid flooding the device with too many packets
}
}
// Close file
fclose(file);
done:
free(value);
}
static void write_value_usage(void)
{
printf("Usage: write-value [options] <value_handle> \n"
"Options:\n"
"\t-w, --without-response\tWrite without response\n"
"\t-s, --signed-write\tSigned write command\n"
"e.g.:\n"
"\twrite-value 0x0001 00 01 00\n");
}
static struct option write_value_options[] = {
{"without-response", 0, 0, 'w'},
{"signed-write", 0, 0, 's'},
{}};
static void write_cb(bool success, uint8_t att_ecode, void *user_data)
{
if (success)
{
PRLOG("\nWrite successful\n");
}
else
{
PRLOG("\nWrite failed: %s (0x%02x)\n",
ecode_to_string(att_ecode), att_ecode);
}
}
static void cmd_write_value(struct client *cli, char *cmd_str)
{
int opt, i, val;
char *argvbuf[516];
char **argv = argvbuf;
int argc = 1;
uint16_t handle;
char *endptr = NULL;
int length;
uint8_t *value = NULL;
bool without_response = false;
bool signed_write = false;
if (!bt_gatt_client_is_ready(cli->gatt))
{
printf("GATT client not initialized\n");
return;
}
if (!parse_args(cmd_str, 514, argv + 1, &argc))
{
printf("Too many arguments\n");
write_value_usage();
return;
}
optind = 0;
argv[0] = "write-value";
while ((opt = getopt_long(argc, argv, "+ws", write_value_options, NULL)) != -1)
{
switch (opt)
{
case 'w':
without_response = true;
break;
case 's':
signed_write = true;
break;
default:
write_value_usage();
return;
}
}
argc -= optind;
argv += optind;
if (argc < 1)
{
write_value_usage();
return;
}
handle = strtol(argv[0], &endptr, 0);
if (!endptr || *endptr != '\0' || !handle)
{
printf("Invalid handle: %s\n", argv[0]);
return;
}
length = argc - 1;
if (length > 0)
{
if (length > UINT16_MAX)
{
printf("Write value too long\n");
return;
}
value = malloc(length);
if (!value)
{
printf("Failed to construct write value\n");
return;
}
for (i = 1; i < argc; i++)
{
val = strtol(argv[i], &endptr, 0);
if (endptr == argv[i] || *endptr != '\0' || errno == ERANGE || val < 0 || val > 255)
{
printf("Invalid value byte: %s\n",
argv[i]);
goto done;
}
value[i - 1] = val;
}
}
if (without_response)
{
if (!bt_gatt_client_write_without_response(cli->gatt, handle,
signed_write, value, length))
{
printf("Failed to initiate write without response "
"procedure\n");
goto done;
}
printf("Write command sent\n");
goto done;
}
if (!bt_gatt_client_write_value(cli->gatt, handle, value, length,
write_cb,
NULL, NULL))
printf("Failed to initiate write procedure\n");
done:
free(value);
}
modify this code to send binary file using blob