{"action":"created","comment":{"url":"https://api.github.com/repos/ARMmbed/ble/pulls/comments/45446819","id":45446819,"diff_hunk":"@@ -210,9 +211,94 @@ class GapAdvertisingData\n      */\n     ble_error_t addData(DataType advDataType, const uint8_t *payload, uint8_t len)\n     {\n-        /* ToDo: Check if an AD type already exists and if the existing */\n-        /*       value is exclusive or not (flags, etc.) */\n+        ble_error_t result = BLE_ERROR_BUFFER_OVERFLOW;\n+\n+        // find field\n+        uint8_t* field = findField(advDataType);\n+\n+        // Field type already exist, either add to field or replace\n+        if (field) {\n+            switch(advDataType) {\n+                case FLAGS:\n+                case SHORTENED_LOCAL_NAME:\n+                case COMPLETE_LOCAL_NAME:\n+                case TX_POWER_LEVEL:\n+                case DEVICE_ID:\n+                case SLAVE_CONNECTION_INTERVAL_RANGE:\n+                case SERVICE_DATA:\n+                case APPEARANCE:\n+                case ADVERTISING_INTERVAL:\n+                case MANUFACTURER_SPECIFIC_DATA: {\n+                    // current field length, with the type subtracted\n+                    uint8_t dataLength = field[0] - 1;\n+\n+                    // new data has same length, do in-order replacement\n+                    if (len == dataLength) {\n+                        for (uint8_t idx = 0; idx < dataLength; idx++) {\n+                            field[2 + idx] = payload[idx];\n+                        }\n+                    } else {\n+                        // check if data fits\n+                        if ((_payloadLen - dataLength + len) <= GAP_ADVERTISING_DATA_MAX_PAYLOAD) {\n+                            // remove old field\n+                            while (field + dataLength < &_payload[GAP_ADVERTISING_DATA_MAX_PAYLOAD]) {\n+                                *field = field[dataLength];\n+                                field++;\n+                            }\n+                            // add new field\n+                            result = appendField(advDataType, payload, len);\n+                        }\n+                    }\n+\n+                    break;\n+                }\n+                case INCOMPLETE_LIST_16BIT_SERVICE_IDS:\n+                case COMPLETE_LIST_16BIT_SERVICE_IDS:\n+                case INCOMPLETE_LIST_32BIT_SERVICE_IDS:\n+                case COMPLETE_LIST_32BIT_SERVICE_IDS:\n+                case INCOMPLETE_LIST_128BIT_SERVICE_IDS:\n+                case COMPLETE_LIST_128BIT_SERVICE_IDS:\n+                case LIST_128BIT_SOLICITATION_IDS: {\n+                    // check if data fits\n+                    if ((_payloadLen + len) <= GAP_ADVERTISING_DATA_MAX_PAYLOAD) {\n+                        // make room for new field\n+                        uint8_t* end = &_payload[_payloadLen];\n+\n+                        while (field < end) {\n+                            end[len] = *end;\n+                            end--;\n+                        }\n+\n+                        // insert new data\n+                        for (uint8_t idx = 0; idx < len; idx++) {\n+                            field[2 + idx] = payload[idx];\n+                        }\n+\n+                        // increment lengths\n+                        field[0] += len;\n+                        _payloadLen += len;\n+\n+                        result = BLE_ERROR_NONE;\n+                    }\n+\n+                    break;\n+                }\n+                default:\n+                    break;\n+            }\n+        } else {\n+            // field doesn't exists, insert new\n+            result = appendField(advDataType, payload, len);\n+        }\n+\n+        return result;\n+    }\n \n+    /**\n+     * Append advertising data based on the specified AD type (see DataType)\n+     */\n+    ble_error_t appendField(DataType advDataType, const uint8_t *payload, uint8_t len)","path":"ble/GapAdvertisingData.h","position":100,"original_position":100,"commit_id":"482f22d4494e2ebfd3898a1025e4a708d29c61ed","original_commit_id":"482f22d4494e2ebfd3898a1025e4a708d29c61ed","user":{"login":"rgrover","id":112704,"avatar_url":"https://avatars.githubusercontent.com/u/112704?v=3","gravatar_id":"","url":"https://api.github.com/users/rgrover","html_url":"https://github.com/rgrover","followers_url":"https://api.github.com/users/rgrover/followers","following_url":"https://api.github.com/users/rgrover/following{/other_user}","gists_url":"https://api.github.com/users/rgrover/gists{/gist_id}","starred_url":"https://api.github.com/users/rgrover/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rgrover/subscriptions","organizations_url":"https://api.github.com/users/rgrover/orgs","repos_url":"https://api.github.com/users/rgrover/repos","events_url":"https://api.github.com/users/rgrover/events{/privacy}","received_events_url":"https://api.github.com/users/rgrover/received_events","type":"User","site_admin":false},"body":"ok. then that's fine.","created_at":"2015-11-20T08:49:44Z","updated_at":"2015-11-20T08:49:44Z","html_url":"https://github.com/ARMmbed/ble/pull/101#discussion_r45446819","pull_request_url":"https://api.github.com/repos/ARMmbed/ble/pulls/101","_links":{"self":{"href":"https://api.github.com/repos/ARMmbed/ble/pulls/comments/45446819"},"html":{"href":"https://github.com/ARMmbed/ble/pull/101#discussion_r45446819"},"pull_request":{"href":"https://api.github.com/repos/ARMmbed/ble/pulls/101"}}},"pull_request":{"url":"https://api.github.com/repos/ARMmbed/ble/pulls/101","id":49936009,"html_url":"https://github.com/ARMmbed/ble/pull/101","diff_url":"https://github.com/ARMmbed/ble/pull/101.diff","patch_url":"https://github.com/ARMmbed/ble/pull/101.patch","issue_url":"https://api.github.com/repos/ARMmbed/ble/issues/101","number":101,"state":"open","locked":false,"title":"Changed GapAdvertisingData.addData to take current fields into account when adding data.","user":{"login":"marcuschangarm","id":7205609,"avatar_url":"https://avatars.githubusercontent.com/u/7205609?v=3","gravatar_id":"","url":"https://api.github.com/users/marcuschangarm","html_url":"https://github.com/marcuschangarm","followers_url":"https://api.github.com/users/marcuschangarm/followers","following_url":"https://api.github.com/users/marcuschangarm/following{/other_user}","gists_url":"https://api.github.com/users/marcuschangarm/gists{/gist_id}","starred_url":"https://api.github.com/users/marcuschangarm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marcuschangarm/subscriptions","organizations_url":"https://api.github.com/users/marcuschangarm/orgs","repos_url":"https://api.github.com/users/marcuschangarm/repos","events_url":"https://api.github.com/users/marcuschangarm/events{/privacy}","received_events_url":"https://api.github.com/users/marcuschangarm/received_events","type":"User","site_admin":false},"body":"Fields with one value are overwritten, fields that can hold multiple values are aggregated.","created_at":"2015-11-06T10:11:16Z","updated_at":"2015-11-20T08:49:44Z","closed_at":null,"merged_at":null,"merge_commit_sha":"bdb7468dc76ebea791d6a043c97d85acb809e72e","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/ARMmbed/ble/pulls/101/commits","review_comments_url":"https://api.github.com/repos/ARMmbed/ble/pulls/101/comments","review_comment_url":"https://api.github.com/repos/ARMmbed/ble/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ARMmbed/ble/issues/101/comments","statuses_url":"https://api.github.com/repos/ARMmbed/ble/statuses/482f22d4494e2ebfd3898a1025e4a708d29c61ed","head":{"label":"marcuschangarm:advertising","ref":"advertising","sha":"482f22d4494e2ebfd3898a1025e4a708d29c61ed","user":{"login":"marcuschangarm","id":7205609,"avatar_url":"https://avatars.githubusercontent.com/u/7205609?v=3","gravatar_id":"","url":"https://api.github.com/users/marcuschangarm","html_url":"https://github.com/marcuschangarm","followers_url":"https://api.github.com/users/marcuschangarm/followers","following_url":"https://api.github.com/users/marcuschangarm/following{/other_user}","gists_url":"https://api.github.com/users/marcuschangarm/gists{/gist_id}","starred_url":"https://api.github.com/users/marcuschangarm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marcuschangarm/subscriptions","organizations_url":"https://api.github.com/users/marcuschangarm/orgs","repos_url":"https://api.github.com/users/marcuschangarm/repos","events_url":"https://api.github.com/users/marcuschangarm/events{/privacy}","received_events_url":"https://api.github.com/users/marcuschangarm/received_events","type":"User","site_admin":false},"repo":{"id":45320185,"name":"ble","full_name":"marcuschangarm/ble","owner":{"login":"marcuschangarm","id":7205609,"avatar_url":"https://avatars.githubusercontent.com/u/7205609?v=3","gravatar_id":"","url":"https://api.github.com/users/marcuschangarm","html_url":"https://github.com/marcuschangarm","followers_url":"https://api.github.com/users/marcuschangarm/followers","following_url":"https://api.github.com/users/marcuschangarm/following{/other_user}","gists_url":"https://api.github.com/users/marcuschangarm/gists{/gist_id}","starred_url":"https://api.github.com/users/marcuschangarm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marcuschangarm/subscriptions","organizations_url":"https://api.github.com/users/marcuschangarm/orgs","repos_url":"https://api.github.com/users/marcuschangarm/repos","events_url":"https://api.github.com/users/marcuschangarm/events{/privacy}","received_events_url":"https://api.github.com/users/marcuschangarm/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/marcuschangarm/ble","description":"API to abstract working with Bluetooth Smart Controllers","fork":true,"url":"https://api.github.com/repos/marcuschangarm/ble","forks_url":"https://api.github.com/repos/marcuschangarm/ble/forks","keys_url":"https://api.github.com/repos/marcuschangarm/ble/keys{/key_id}","collaborators_url":"https://api.github.com/repos/marcuschangarm/ble/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/marcuschangarm/ble/teams","hooks_url":"https://api.github.com/repos/marcuschangarm/ble/hooks","issue_events_url":"https://api.github.com/repos/marcuschangarm/ble/issues/events{/number}","events_url":"https://api.github.com/repos/marcuschangarm/ble/events","assignees_url":"https://api.github.com/repos/marcuschangarm/ble/assignees{/user}","branches_url":"https://api.github.com/repos/marcuschangarm/ble/branches{/branch}","tags_url":"https://api.github.com/repos/marcuschangarm/ble/tags","blobs_url":"https://api.github.com/repos/marcuschangarm/ble/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/marcuschangarm/ble/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/marcuschangarm/ble/git/refs{/sha}","trees_url":"https://api.github.com/repos/marcuschangarm/ble/git/trees{/sha}","statuses_url":"https://api.github.com/repos/marcuschangarm/ble/statuses/{sha}","languages_url":"https://api.github.com/repos/marcuschangarm/ble/languages","stargazers_url":"https://api.github.com/repos/marcuschangarm/ble/stargazers","contributors_url":"https://api.github.com/repos/marcuschangarm/ble/contributors","subscribers_url":"https://api.github.com/repos/marcuschangarm/ble/subscribers","subscription_url":"https://api.github.com/repos/marcuschangarm/ble/subscription","commits_url":"https://api.github.com/repos/marcuschangarm/ble/commits{/sha}","git_commits_url":"https://api.github.com/repos/marcuschangarm/ble/git/commits{/sha}","comments_url":"https://api.github.com/repos/marcuschangarm/ble/comments{/number}","issue_comment_url":"https://api.github.com/repos/marcuschangarm/ble/issues/comments{/number}","contents_url":"https://api.github.com/repos/marcuschangarm/ble/contents/{+path}","compare_url":"https://api.github.com/repos/marcuschangarm/ble/compare/{base}...{head}","merges_url":"https://api.github.com/repos/marcuschangarm/ble/merges","archive_url":"https://api.github.com/repos/marcuschangarm/ble/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/marcuschangarm/ble/downloads","issues_url":"https://api.github.com/repos/marcuschangarm/ble/issues{/number}","pulls_url":"https://api.github.com/repos/marcuschangarm/ble/pulls{/number}","milestones_url":"https://api.github.com/repos/marcuschangarm/ble/milestones{/number}","notifications_url":"https://api.github.com/repos/marcuschangarm/ble/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/marcuschangarm/ble/labels{/name}","releases_url":"https://api.github.com/repos/marcuschangarm/ble/releases{/id}","created_at":"2015-10-31T22:16:54Z","updated_at":"2015-10-31T22:16:55Z","pushed_at":"2015-11-05T11:46:08Z","git_url":"git://github.com/marcuschangarm/ble.git","ssh_url":"git@github.com:marcuschangarm/ble.git","clone_url":"https://github.com/marcuschangarm/ble.git","svn_url":"https://github.com/marcuschangarm/ble","homepage":null,"size":1298,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"ARMmbed:develop","ref":"develop","sha":"73559d3befb8c870e14224fcecafb760bd5c70c0","user":{"login":"ARMmbed","id":8916057,"avatar_url":"https://avatars.githubusercontent.com/u/8916057?v=3","gravatar_id":"","url":"https://api.github.com/users/ARMmbed","html_url":"https://github.com/ARMmbed","followers_url":"https://api.github.com/users/ARMmbed/followers","following_url":"https://api.github.com/users/ARMmbed/following{/other_user}","gists_url":"https://api.github.com/users/ARMmbed/gists{/gist_id}","starred_url":"https://api.github.com/users/ARMmbed/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ARMmbed/subscriptions","organizations_url":"https://api.github.com/users/ARMmbed/orgs","repos_url":"https://api.github.com/users/ARMmbed/repos","events_url":"https://api.github.com/users/ARMmbed/events{/privacy}","received_events_url":"https://api.github.com/users/ARMmbed/received_events","type":"Organization","site_admin":false},"repo":{"id":26317661,"name":"ble","full_name":"ARMmbed/ble","owner":{"login":"ARMmbed","id":8916057,"avatar_url":"https://avatars.githubusercontent.com/u/8916057?v=3","gravatar_id":"","url":"https://api.github.com/users/ARMmbed","html_url":"https://github.com/ARMmbed","followers_url":"https://api.github.com/users/ARMmbed/followers","following_url":"https://api.github.com/users/ARMmbed/following{/other_user}","gists_url":"https://api.github.com/users/ARMmbed/gists{/gist_id}","starred_url":"https://api.github.com/users/ARMmbed/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ARMmbed/subscriptions","organizations_url":"https://api.github.com/users/ARMmbed/orgs","repos_url":"https://api.github.com/users/ARMmbed/repos","events_url":"https://api.github.com/users/ARMmbed/events{/privacy}","received_events_url":"https://api.github.com/users/ARMmbed/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ARMmbed/ble","description":"API to abstract working with Bluetooth Smart Controllers","fork":false,"url":"https://api.github.com/repos/ARMmbed/ble","forks_url":"https://api.github.com/repos/ARMmbed/ble/forks","keys_url":"https://api.github.com/repos/ARMmbed/ble/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ARMmbed/ble/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ARMmbed/ble/teams","hooks_url":"https://api.github.com/repos/ARMmbed/ble/hooks","issue_events_url":"https://api.github.com/repos/ARMmbed/ble/issues/events{/number}","events_url":"https://api.github.com/repos/ARMmbed/ble/events","assignees_url":"https://api.github.com/repos/ARMmbed/ble/assignees{/user}","branches_url":"https://api.github.com/repos/ARMmbed/ble/branches{/branch}","tags_url":"https://api.github.com/repos/ARMmbed/ble/tags","blobs_url":"https://api.github.com/repos/ARMmbed/ble/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ARMmbed/ble/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ARMmbed/ble/git/refs{/sha}","trees_url":"https://api.github.com/repos/ARMmbed/ble/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ARMmbed/ble/statuses/{sha}","languages_url":"https://api.github.com/repos/ARMmbed/ble/languages","stargazers_url":"https://api.github.com/repos/ARMmbed/ble/stargazers","contributors_url":"https://api.github.com/repos/ARMmbed/ble/contributors","subscribers_url":"https://api.github.com/repos/ARMmbed/ble/subscribers","subscription_url":"https://api.github.com/repos/ARMmbed/ble/subscription","commits_url":"https://api.github.com/repos/ARMmbed/ble/commits{/sha}","git_commits_url":"https://api.github.com/repos/ARMmbed/ble/git/commits{/sha}","comments_url":"https://api.github.com/repos/ARMmbed/ble/comments{/number}","issue_comment_url":"https://api.github.com/repos/ARMmbed/ble/issues/comments{/number}","contents_url":"https://api.github.com/repos/ARMmbed/ble/contents/{+path}","compare_url":"https://api.github.com/repos/ARMmbed/ble/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ARMmbed/ble/merges","archive_url":"https://api.github.com/repos/ARMmbed/ble/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ARMmbed/ble/downloads","issues_url":"https://api.github.com/repos/ARMmbed/ble/issues{/number}","pulls_url":"https://api.github.com/repos/ARMmbed/ble/pulls{/number}","milestones_url":"https://api.github.com/repos/ARMmbed/ble/milestones{/number}","notifications_url":"https://api.github.com/repos/ARMmbed/ble/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ARMmbed/ble/labels{/name}","releases_url":"https://api.github.com/repos/ARMmbed/ble/releases{/id}","created_at":"2014-11-07T11:50:32Z","updated_at":"2015-11-11T23:52:15Z","pushed_at":"2015-11-19T15:34:30Z","git_url":"git://github.com/ARMmbed/ble.git","ssh_url":"git@github.com:ARMmbed/ble.git","clone_url":"https://github.com/ARMmbed/ble.git","svn_url":"https://github.com/ARMmbed/ble","homepage":null,"size":1648,"stargazers_count":26,"watchers_count":26,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":39,"mirror_url":null,"open_issues_count":21,"forks":39,"open_issues":21,"watchers":26,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ARMmbed/ble/pulls/101"},"html":{"href":"https://github.com/ARMmbed/ble/pull/101"},"issue":{"href":"https://api.github.com/repos/ARMmbed/ble/issues/101"},"comments":{"href":"https://api.github.com/repos/ARMmbed/ble/issues/101/comments"},"review_comments":{"href":"https://api.github.com/repos/ARMmbed/ble/pulls/101/comments"},"review_comment":{"href":"https://api.github.com/repos/ARMmbed/ble/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ARMmbed/ble/pulls/101/commits"},"statuses":{"href":"https://api.github.com/repos/ARMmbed/ble/statuses/482f22d4494e2ebfd3898a1025e4a708d29c61ed"}}}}