summaryrefslogtreecommitdiff
path: root/dacp.c
diff options
context:
space:
mode:
authorMike Brady <mikebrady@eircom.net>2018-04-08 12:36:00 +0100
committerMike Brady <mikebrady@eircom.net>2018-04-08 12:36:00 +0100
commit7fb8913532a7be943fef38ed98a91f541596dc07 (patch)
tree0717d29743804f84112d47f02da552dda3ac111f /dacp.c
parentc937b50c52afd0b6ed71c3acac5ff1ade0ad4ebd (diff)
Use snprintf in place of sprintf and initialise mutexes for debugging.
Diffstat (limited to 'dacp.c')
-rw-r--r--dacp.c74
1 files changed, 60 insertions, 14 deletions
diff --git a/dacp.c b/dacp.c
index b0afb18..e22ac63 100644
--- a/dacp.c
+++ b/dacp.c
@@ -113,8 +113,10 @@ static const struct http_funcs responseFuncs = {
response_realloc, response_body, response_header, response_code,
};
-static pthread_mutex_t dacp_conversation_lock = PTHREAD_MUTEX_INITIALIZER;
-static pthread_mutex_t dacp_server_information_lock = PTHREAD_MUTEX_INITIALIZER;
+// static pthread_mutex_t dacp_conversation_lock = PTHREAD_MUTEX_INITIALIZER;
+// static pthread_mutex_t dacp_server_information_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t dacp_conversation_lock;
+static pthread_mutex_t dacp_server_information_lock;
static pthread_cond_t dacp_server_information_cv = PTHREAD_COND_INITIALIZER;
int dacp_send_command(const char *command, char **body, ssize_t *bodysize) {
@@ -139,14 +141,14 @@ int dacp_send_command(const char *command, char **body, ssize_t *bodysize) {
response.size = 0;
response.code = 0;
- char portstring[10], server[256], message[1024];
+ char portstring[10], server[1024], message[1024];
memset(&portstring, 0, sizeof(portstring));
if (dacp_server.connection_family == AF_INET6) {
- sprintf(server, "%s%%%u", dacp_server.ip_string, dacp_server.scope_id);
+ snprintf(server, sizeof(server), "%s%%%u", dacp_server.ip_string, dacp_server.scope_id);
} else {
strcpy(server, dacp_server.ip_string);
}
- sprintf(portstring, "%u", dacp_server.port);
+ snprintf(portstring, sizeof(portstring), "%u", dacp_server.port);
// first, load up address structs with getaddrinfo():
@@ -192,9 +194,9 @@ int dacp_send_command(const char *command, char **body, ssize_t *bodysize) {
} else {
// debug(1,"DACP connect succeeded.");
- sprintf(message,
- "GET /ctrl-int/1/%s HTTP/1.1\r\nHost: %s:%u\r\nActive-Remote: %u\r\n\r\n",
- command, dacp_server.ip_string, dacp_server.port, dacp_server.active_remote_id);
+ snprintf(message, sizeof(message),
+ "GET /ctrl-int/1/%s HTTP/1.1\r\nHost: %s:%u\r\nActive-Remote: %u\r\n\r\n",
+ command, dacp_server.ip_string, dacp_server.port, dacp_server.active_remote_id);
// Send command
// debug(1,"DACP connect message: \"%s\".",message);
@@ -221,7 +223,7 @@ int dacp_send_command(const char *command, char **body, ssize_t *bodysize) {
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof tv) == -1)
debug(1, "Error %d setting receive timeout for DACP service.", errno);
int ndata = recv(sockfd, buffer, sizeof(buffer), 0);
- // debug(1,"Received %d bytes: \"%s\".",ndata,buffer);
+ debug(3, "Received %d bytes: \"%s\".", ndata, buffer);
if (ndata <= 0) {
debug(1, "dacp_send_command -- error receiving response for command \"%s\".",
command);
@@ -422,7 +424,7 @@ void *dacp_monitor_thread_code(__attribute__((unused)) void *na) {
else
idle_scan_count = 0;
- debug(3, "Scan Result: %d, Bad Scan Count: %d, Idle Scan Count: %d.", result, bad_result_count,
+ debug(2, "Scan Result: %d, Bad Scan Count: %d, Idle Scan Count: %d.", result, bad_result_count,
idle_scan_count);
if ((bad_result_count == config.scan_max_bad_response_count) ||
@@ -761,6 +763,49 @@ void *dacp_monitor_thread_code(__attribute__((unused)) void *na) {
}
void dacp_monitor_start() {
+ int rc;
+ pthread_mutexattr_t mta;
+
+ rc = pthread_mutexattr_init(&mta);
+ if (rc)
+ debug(1, "Error creating the DACP Conversation Lock Mutex Att Init");
+
+ rc = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK);
+ if (rc)
+ debug(1, "Error creating the DACP Conversation Lock Mutex Errorcheck");
+
+ // rc = pthread_mutexattr_setname_np(&mta, "DACP Conversation Lock");
+ // if (rc)
+ // debug(1,"Error creating the DACP Conversation Lock Mutex Set Name");
+
+ rc = pthread_mutex_init(&dacp_conversation_lock, &mta);
+ if (rc)
+ debug(1, "Error creating the DACP Conversation Lock Mutex Init");
+
+ rc = pthread_mutexattr_destroy(&mta);
+ if (rc)
+ debug(1, "Error creating the DACP Conversation Lock Attr Destroy");
+
+ rc = pthread_mutexattr_init(&mta);
+ if (rc)
+ debug(1, "Error creating the DACP Server Information Lock Mutex Att Init");
+
+ rc = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK);
+ if (rc)
+ debug(1, "Error creating the DACP Server Information Lock Mutex Errorcheck");
+
+ // rc = pthread_mutexattr_setname_np(&mta, "DACP Conversation Lock");
+ // if (rc)
+ // debug(1,"Error creating the DACP Server Information Lock Mutex Set Name");
+
+ rc = pthread_mutex_init(&dacp_server_information_lock, &mta);
+ if (rc)
+ debug(1, "Error creating the DACP Server Information Lock Mutex Init");
+
+ rc = pthread_mutexattr_destroy(&mta);
+ if (rc)
+ debug(1, "Error creating the DACP Server Information Lock Attr Destroy");
+
memset(&dacp_server, 0, sizeof(dacp_server_record));
pthread_create(&dacp_monitor_thread, NULL, dacp_monitor_thread_code, NULL);
}
@@ -821,8 +866,9 @@ int dacp_set_include_speaker_volume(int64_t machine_number, int32_t vo) {
debug(2, "dacp_set_include_speaker_volume to %" PRId32 ".", vo);
char message[1000];
memset(message, 0, sizeof(message));
- sprintf(message, "setproperty?include-speaker-id=%" PRId64 "&dmcp.volume=%" PRId32 "",
- machine_number, vo);
+ snprintf(message, sizeof(message),
+ "setproperty?include-speaker-id=%" PRId64 "&dmcp.volume=%" PRId32 "", machine_number,
+ vo);
debug(2, "sending \"%s\"", message);
return send_simple_dacp_command(message);
// should return 204
@@ -831,8 +877,8 @@ int dacp_set_include_speaker_volume(int64_t machine_number, int32_t vo) {
int dacp_set_speaker_volume(int64_t machine_number, int32_t vo) {
char message[1000];
memset(message, 0, sizeof(message));
- sprintf(message, "setproperty?speaker-id=%" PRId64 "&dmcp.volume=%" PRId32 "", machine_number,
- vo);
+ snprintf(message, sizeof(message), "setproperty?speaker-id=%" PRId64 "&dmcp.volume=%" PRId32 "",
+ machine_number, vo);
debug(2, "sending \"%s\"", message);
return send_simple_dacp_command(message);
// should return 204